template question

M

Marc Schellens

I have a template like:

template< typename B> class A: public B
{
typedef typename B::Ty Ty;
A( Ty&);

A( string);
};

The latter ctor, I need only for one out of several B
But for one B, B::Ty is a string.

So I get an error message.

Question:
Can I define a ctor for only one special A<B> ?
If not is there another solution?

thanks,
marc
 
G

Gianni Mariani

Marc said:
I have a template like:

template< typename B> class A: public B
{
typedef typename B::Ty Ty;
A( Ty&);

A( string);
};

The latter ctor, I need only for one out of several B
But for one B, B::Ty is a string.

You might try a partial specialization "traits" class.


template <typename B> class Traits
{
typedef string OtherConstructorParam;
};

template <> class Traits<string>
{
class Junk OtherConstructorParam;
};


template< typename B> class A: public B
{
typedef typename B::Ty Ty;
A( Ty&);
 
M

Michael Kochetkov

Gianni Mariani said:
You might try a partial specialization "traits" class.


template <typename B> class Traits
{
typedef string OtherConstructorParam;
};

template <> class Traits<string>
{
class Junk OtherConstructorParam;
};
Does not it is called explicit specialization?
 
M

Michael Kochetkov

Marc Schellens said:
I have a template like:

template< typename B> class A: public B
{
typedef typename B::Ty Ty;
A( Ty&);

A( string);
};

The latter ctor, I need only for one out of several B
But for one B, B::Ty is a string.

So I get an error message.

Question:
Can I define a ctor for only one special A<B> ?
If not is there another solution?
struct string {};
struct B { typedef string Ty; };
struct C { typedef int Ty; };

template<class T, class Ty = typename T::Ty>
struct A : public T {
A(Ty&) {}
};

template<class T>
struct A<T,string> {
A(string&) {}
A(string) {}
};


int
main()
{
A<B> a = string();
A<C> a1 = string(); // This will not compile
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top