Sign in to follow this  

Default argument for templated template parameters

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

This topic is 4838 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.

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