• Create Account

# Using type of element in a container as a parameter?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Servant of the Lord  Crossbones+   -  Reputation: 12780

Like
0Likes
Like

Posted 06 October 2012 - 11:10 PM

I have the following code that won't compile:
template<class ContainerType>
void MyFunc(ContainerType &container, ContainerType::value_type fallback = ContainerType::value_type())

It will be passed something like an std::vector<> of unknown element-type.
I want it to take as the second parameter a variable of type 'element type of the container'.
I want the second parameter to default to "default initialized version of said type".

Isn't 'value_type' a standardized C++ container typedef? It's definitely present in the compiler I'm using (MinGW 4.6).

For example, if passing in a std::vector<int> into the function, it should be: std::vector<int>::value_type fallback = std::vector<int>::value_type(), which should be int fallback = int().

However, even before using the template, I get a compile error in any source files that #include the header with the templated function in it.

MinGW errors:
error: 'ContainerType::value_type' is not a type

(This is for: ContainerType::value_type fallback)

error: expected initializer before ')' token

(This is for: = ContainerType::value_type())

Why isn't ContainerType::value_type a type? std::vector<int>::value_type exists in the vector header.

I also tried this:
template<class ContainerType, class ElementType = ContainerType::value_type>
void MyFunc(ContainerType &container, ElementType fallback)

And got these errors:
error: expected type-specifier
error: expected '>'

Where is my thinking going awry?

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #2wqking  Members   -  Reputation: 754

Like
2Likes
Like

Posted 06 October 2012 - 11:30 PM

I think you missed typename before ContainerType::value_type

template <class ContainerType>

void MyFunc(ContainerType &container, typename ContainerType::value_type fallback = typename ContainerType::value_type())



Edited by wqking, 06 October 2012 - 11:34 PM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.

### #3Servant of the Lord  Crossbones+   -  Reputation: 12780

Like
0Likes
Like

Posted 07 October 2012 - 02:08 PM

Yep, thanks a bunch!

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS