Jump to content
  • Advertisement
Sign in to follow this  
CodeStorm

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

This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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]

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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

[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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!