Jump to content
  • Advertisement
Sign in to follow this  
joanusdmentia

Default argument for templated template parameters

This topic is 5175 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

This is really starting to give me the shits. Can anyone see a problem with this code? The problem is with the default argument for the templated template parameter Storage.
namespace detail
{
    template<std::size_t Size> class VectorStorageN
    {
    public:
        template<typename Type> class StorageN
        {
            //...
        };
    };
} // namespace detail


template<std::size_t Size, typename Type = real, template<typename> class Storage = detail::VectorStorageN<Size>::StorageN>
class Vector : public Storage<Type>
{
    //...
};



I can't seem to get around the error
error C3202: 'StorageN' : invalid default argument for template parameter '<Unknown>', expected a class template
EDIT: I'm using VC++ 7.1

Share this post


Link to post
Share on other sites
Advertisement
Are you trying to accomplish something like this?

template<std::size_t Size> class VectorStorageN
{
template<typename Type> class StorageN
{
};
};

template<std::size_t Size, typename Type=real, typename Storage = VectorStorageN<Size>::StorageN<Type> >
class Vector : public Storage
{
};



EDIT: Explanation: you don't need to specify the type 'Storage' as a templated type, because the type is evaluated at compile-time and therefore can be anything you want, including templated classes. Hope this is right, if not, feel free to correct ;)

Share this post


Link to post
Share on other sites
It *appears* to me that omitting a name for the template template parameter would cause the compiler to baulk at parsing the ttp.


template<std::size_t Size, typename Type = real, template<typename T> class Storage = detail::VectorStorageN<T>::StorageN>
class Vector : public Storage<Type>
{
//...
};



But, since I certainly don't know everything there is to know about C++ (far from it) I could be wrong.

Of course, if it is as sphinx23 states (he posted while I was writing), then wouldn't it be:


template<std::size_t Size, typename Type = real, template<typename T, typename S> class Storage = detail::VectorStorageN<S>::StorageN<T> >
class Vector : public Storage<Size,Type>
{
//...
};


One question though... why do you need to inherit from storage in this manner? Why not simply aggregate it?

Cheers,

Timkin

Share this post


Link to post
Share on other sites
Quote:
Original post by sphinx23
Are you trying to accomplish something like this?
*** Source Snippet Removed ***

EDIT: Explanation: you don't need to specify the type 'Storage' as a templated type, because the type is evaluated at compile-time and therefore can be anything you want, including templated classes. Hope this is right, if not, feel free to correct ;)

Nope, not quite what I'm after. I do need Storage to be templated. Here's what I'm trying to do:

namespace detail
{
template<std::size_t Size> class VectorStorageN
{
public:
template<typename Type> class StorageN
{
public:
//...
Type data[Size];
};
};

template<typename Type> class VectorStorageXYZ
{
public:
//...
Type x,y,z;
}
} // namespace detail


template<std::size_t Size, typename Type = real, template<typename> class Storage = detail::VectorStorageN<Size>::StorageN>
class Vector : public Storage<Type>
{
//...
};

typedef Vector<3,float,detail::VectorStorageXYZ> Vector3;

Vector3 someVector;
Vector<3,int> anotherVector;





Previously I was instantiating the storage class in the typedef, but this allowed for the storage class and vector class to have different types....obviously a bad thing. Trying to fix this I ran into this problem.

Quote:
Original post by Timkin
It *appears* to me that omitting a name for the template template parameter would cause the compiler to baulk at parsing the ttp.

Already tried it to no avail :(

I'm really quite surprised this is even generating an error. Could it be a MSVC quirk?

Quote:
Original post by Timkin
One question though... why do you need to inherit from storage in this manner? Why not simply aggregate it?

The extra code above probably cleared this one up, but doing this allows me to customise my vector so that I can access the components as v.x,v.y,v.z or v.r,v.g,v.b or whatever else proves useful. Not essential, but can make things that little bit more readable. I'm also using the technique in this thread to effectively achieve an anonymous union of the components with an array of Type in a standard compliant way, allowing me to have indexing as well without needing a switch statement to return each component that corresponds to an index number.

Share this post


Link to post
Share on other sites
Yep, it's an MSVC quirk. Just tried it with GCC 3.2 and it compiled fine. Oh well, looks like I'm back to manually instantiating the storage class in the typedef.

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!