Sign in to follow this  
CodeStorm

Unity Obtaining the type of elements within a vector of vectors.

Recommended Posts

CodeStorm    127
[size="2"][color="black"][font="Arial"][size="2"]Lately, I've been making use of std::vector's'value_type' typedefs in function parameters, so that code written thus;

[code]
template<typename TypeA, typename TypeB>
void Foo
(
const TypeA & A,
const TypeB & B
)
{
}
[/code]

can be rewritten as;

[code]
template<typename Type>
void Foo
(
const Type & A,
typename Type::value_type b
)
{
}
[/code]

for types that deploy the 'value_type' typedef. However, when it comes onto types that are vectors of vectors then code like this;

[code]
template<typename Type>
void Foo
(
const Type & A,
typename Type::value_type::size_type b
)
{
}
[/code]

compiles without any issues, where as the following code;

[code]
template<typename Type>
void Foo
(
const Type & A,
typename Type::value_type::value_type b
)
{
}
[/code]

does not. When nesting value_type like this, my MSV C++ 2010 Express compiler comes up with:

error C2893: Failed to specialize function template 'void Foo(const Type&,Type::value_type::{ctor})

There are obvious work arounds regarding this problem (like simply using another type parameter in the template), but I would just like to know why one type of nesting works and not the other.

And besides... Template programming is a bit of a chink in my C++ armour at the moment, so any opportunity to gain a heads up would be much appreciated![/size][/font][/color]

[/size]

Share this post


Link to post
Share on other sites
Wooh    1088
I don't know why it doesn't work for you. This code works for me
[code]#include <vector>

template<typename Type>
void Foo
(
const Type & A,
typename Type::value_type::value_type b
)
{
}

int main()
{
std::vector<std::vector<int> > v;
Foo(v, 4);
}[/code]

Share this post


Link to post
Share on other sites
japro    887
I also checked and it works with gcc 4.4.3 and intel 12.0.2.
[code]
#include <iostream>
#include <vector>

template<class V>
void foo(const V &v, typename V::value_type::value_type b)
{
std::cout << b << std::endl;
}

int main(int argc, char *arvg[])
{

std::vector<std::vector<int> > vec;
foo(vec, 45);

return 0;
}
[/code]


Does this work? (just guessing)
[code]
template <class T>
struct funky_workaround { typedef typename T::value_type value_type; };

template<class V>
void foo(const V &v, typename funky_workaround<typename V::value_type>::value_type b)
{
std::cout << b << std::endl;
}
[/code]

Share this post


Link to post
Share on other sites
CodeStorm    127
Yeah... Checked and rechecked my code again. Still no joy!

I even used your version Wooh just to make sure... And it still won't compile...

But thanks japro! That funky work around of yours did do the trick... But it sure is... errr... funky LOL!

In any case, the fact that my "bugged" code compiles for some of you indicates that something might not be right with my compiler (that or it needs upgrading).

Anyway, thanks for replies ;-)

BTW Wooh... Just out of curiosity, what compiler are you using?

Share this post


Link to post
Share on other sites
Wooh    1088
[quote name='CodeStorm' timestamp='1313107340' post='4848014']BTW Wooh... Just out of curiosity, what compiler are you using?[/quote]
gcc 4.2.4

Share this post


Link to post
Share on other sites
CodeStorm    127
[quote name='Wooh' timestamp='1313108512' post='4848018']
[quote name='CodeStorm' timestamp='1313107340' post='4848014']BTW Wooh... Just out of curiosity, what compiler are you using?[/quote]
gcc 4.2.4
[/quote]


LOL! Thanks... Yeah that figures I guess...

Just been checking out MS Connect which apparently has a few people with issues regarding [font=Verdana][size=2][b]C2893[/b][/size][/font].

So it seems MSV C++ Express 2010 still has a few bugs in it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this