• Advertisement
Sign in to follow this  

Quick C++ question.

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

Hello, I have a quick C++ question. Ok, so I have a template class (Bar in this case) that takes three template arguments: A, B, and C. By design C is also a template class that takes two template arguments. In the design, C's two template argument types must be the same as A, and B. I know you can pass them properly, but is there a way to enforce this? Something like:
[source lang = "C++"]
template
<
class A,
class B,
template <class A , class B> class C 
>
class Bar
{
public:
};

// Or ...

template
<
class A,
class B,
class C<A, B> 
>
class Bar
{
public:
};


Thank you for your help. Jeremy

Share this post


Link to post
Share on other sites
Advertisement
I haven't tested this, but I believe the syntax you are looking for is:


template
<
class A,
class B,
template <typename A, typename B> class C
>
class Bar
{
public:
};






edit:
I'm sorry, I'm looking at it again and that won't do what you want at all, all that does is force the user to give a template that can take 2 template arguments. I'm a little too tired to remember if what you want can be done at all right now ... also just out of curiosity, why do you need to enforce this?

Share this post


Link to post
Share on other sites
I need to enforce that they are the same because the types need to match in order to have absolutely reliable/predictible results. C is a user defined class and may differ, i.e. it is not always the same class.

It was mentioned that the following would work but I have not updated my compiler to support it yet so I cannot test it til after Christmas:

[source lang = "C++"]
template<typename A, typename B>
class Two
{};

template<typename A, typename B, template <typename, typename> class C>
class Combine
{
public:
typedef C<A, B> result;
};

int main(int argc, char * argv[])
{
std::cout<< typeid( Combine<int, char, Two>::result ).name() <<"\n";
}



If someone has VC 2008 I would love to hear input as to whether this is correct. I will not have VC 2008 til after Christmas and do not want to download the demo since I am going to purchase it soon anyways.

Thank you for your help.
Jeremy

Share this post


Link to post
Share on other sites
Don't require two things to be the same when you can infer one from the other:


template <typename A, typename B>
class C_implementation {
typedef typename A a_type;
typedef typename B b_type;
};

template <typename C>
class Bar {
// Whereever you would have referred to A, refer instead to C::a_type
// Whereever you would have referred to B, refer instead to C::b_type
};

Bar<C_implementation> foo;
// your code will only compile if the provided Bar template argument defines the required typedefs

Share this post


Link to post
Share on other sites
Quote:
Original post by grill8
*** Source Snippet Removed ***

If someone has VC 2008 I would love to hear input as to whether this is correct. I will not have VC 2008 til after Christmas and do not want to download the demo since I am going to purchase it soon anyways.

Thank you for your help.
Jeremy


With VC++ 2008 Express it works out fine, if you include <iostream> (but that's obvious). It outputs:
class Two<int,char>
Emiel1

Share this post


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

  • Advertisement