0

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?

4
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by firstPerson
Featured Replies
  • 1
    Narue 5,707   6 Years Ago

    [QUOTE]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.[/QUOTE] 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(). … Read More

1

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 ?

Edited by Narue: n/a

0

>>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?

0

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>

Edited by nocloud: n/a

0

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);
}
0

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.

Edited by firstPerson: n/a

0

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.

0

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?

0

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.

0

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?

Edited by Narue: n/a

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.