I have a the following snippet of code

string const& symbol::at(int index) const {
  assert(index<symbol_data.vector::size());
  return symbol_data.vector::at(index);
}

This does not compile successfully, instead, I get complaints about

error: ‘template<class _Tp, class _Alloc> class std::vector’ used without template parameters

Now, I think this has something to do with the fact that I'm returning a const reference to an object that this function is treating as const.

How can I modify my two lines of code to satisfy const correctness?

Recommended Answers

All 11 Replies

I think this has something to do with the fact that I'm returning a const reference to an object that this function is treating as const.

I think you're smoking crack if you made that connection with the given error. The problem is your syntax in calling size() and at(). Tell me, what is symbol_data.vector ?

@Narue, maybe it's just a poor choice of variable name?

>>error: ‘template<class _Tp, class _Alloc> class std::vector’ used without template parameters
This error tells you your problem. You are using std::vector without templates. Why are you confused?

What do you mean by using std::vector without templates?

If I remove the the const reference return, the code works perfectly, e.g. with prototype

string symbol::at(int index) {...}

Thus, I am fairly certain this has something to do with the fact that I'm using const in there.

I should also clarify, symbol_data is a vector<string>

I should also clarify, symbol_data is a vector<string>

Then the code should look like this:

string const& symbol::at(int index) const {
  assert(index < symbol_data.size());
  return symbol_data.at(index);
}

Then the code should look like this:

string const& symbol::at(int index) const {
  assert(index < symbol_data.size());
  return symbol_data.at(index);
}

@OP this might be more clearer:

const string& symbol::at(int index) const {
  assert(index < symbol_data.size());
  return symbol_data.at(index);
}

So you can read it as 'constant string-reference' instead of 'string const-reference'. Both the produces the same result, take your pick.

So you can read it as 'constant string-reference' instead of 'string const-reference'.

Which goes against the more consistent policy of reading right to left. Both are valid, and since the OP's code used const on the right, I maintained that style.

Which goes against the more consistent policy of reading right to left. Both are valid, and since the OP's code used const on the right, I maintained that style.

Really? I thought it was more naturally to read things from left to right?

Strangely enough, removing vector:: in the code as shown by Narue does fix the problem although I cannot really understand why this is the case (since it should be calling vector::at either way...). Anyways, thanks for the help.

I thought it was more naturally to read things from left to right?

Let's give it a try:

int x;
x is an int.

int *p;
p is a pointer to int.

int const *p;
p is a pointer to const int.

int const * const p;
p is a const pointer to const int.

Now do the same thing with const to the left and reading left to right:

int x;
int named x.

int *p;
int pointer named p.

const int *p;
const int pointer named p?

const int * const p;
const int pointer that is const named p?

Let's give it a try:

int x;
x is an int.

int *p;
p is a pointer to int.

int const *p;
p is a pointer to const int.

int const * const p;
p is a const pointer to const int.

Now do the same thing with const to the left and reading left to right:

int x;
int named x.

int *p;
int pointer named p.

const int *p;
const int pointer named p?

const int * const p;
const int pointer that is const named p?

I guess I'm just weird then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.