By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

template question

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Marc Schellens wrote:
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&);

A(typename Traits<B>::OtherConstructorParam);
};

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


Jul 19 '05 #2

P: n/a

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...
Marc Schellens wrote:
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;
};

Does not it is called explicit specialization?

--
Michael Kochetkov.
Jul 19 '05 #3

P: n/a
Michael Kochetkov wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...
Marc Schellens wrote:
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;
};


Does not it is called explicit specialization?


Yes. My bad.

Jul 19 '05 #4

P: n/a

"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
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
}

--
Michael Kochetkov.
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.