954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

converting STL vector size_type to const?

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?

nocloud
Newbie Poster
19 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
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
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

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

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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

nocloud
Newbie Poster
19 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
I should also clarify, symbol_data is a vector


Then the code should look like this:

string const& symbol::at(int index) const {
  assert(index < symbol_data.size());
  return symbol_data.at(index);
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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.

nocloud
Newbie Poster
19 posts since Feb 2010
Reputation Points: 10
Solved Threads: 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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: