Obtaining the type of elements within a vector of vectors.

Started by
4 comments, last by CodeStorm 12 years, 8 months ago
[color="black"][font="Arial"]Lately, I've been making use of std::vector's'value_type' typedefs in function parameters, so that code written thus;


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


can be rewritten as;


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


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


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


compiles without any issues, where as the following code;


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


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![/font]

Advertisement
I don't know why it doesn't work for you. This code works for me
#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);
}
I also checked and it works with gcc 4.4.3 and intel 12.0.2.

#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;
}



Does this work? (just guessing)

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;
}
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?
BTW Wooh... Just out of curiosity, what compiler are you using?

gcc 4.2.4

[quote name='CodeStorm' timestamp='1313107340' post='4848014']BTW Wooh... Just out of curiosity, what compiler are you using?

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]C2893[/font].

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

This topic is closed to new replies.

Advertisement