Connecting Tech Pros Worldwide Help | Site Map

Template specialisation with multiple typenames

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 25th, 2007, 09:55 AM
Adam Nielsen
Guest
 
Posts: n/a
Default Template specialisation with multiple typenames

Hi everyone,

I've having some trouble trying to specialise a template. My original
code, with one typename, worked:

template <typename T>
class MyClass
{
private:
T data;

public:
const std::string& toString() const
{
// convert this->data to a string and return
}
};

const std::string& MyClass<std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}

However I then tried to extend the class with a second template
parameter, and it stopped working:

template <class C, typename T>
class MyClass
...

template <class C>
const std::string& MyClass<C, std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}

It would appear to work if I write code for every possible value of C,
but then that's the whole reason for having a template! I'm assuming
I've just gotten the syntax wrong, so if someone would be kind enough to
point out the correct way of doing this it would be much appreciated!

Many thanks,
Adam.

  #2  
Old September 25th, 2007, 11:05 AM
Barry
Guest
 
Posts: n/a
Default Re: Template specialisation with multiple typenames

Adam Nielsen wrote:
Quote:
Hi everyone,
>
I've having some trouble trying to specialise a template. My original
code, with one typename, worked:
>
template <typename T>
class MyClass
{
private:
T data;
>
public:
const std::string& toString() const
{
// convert this->data to a string and return
}
};
>
const std::string& MyClass<std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}
>
However I then tried to extend the class with a second template
parameter, and it stopped working:
>
template <class C, typename T>
class MyClass
class MyClass<Type1>


for example:

template <class F = void()>
class Func
{
};

template <class Ret, class Arg>
class Func<Ret(Arg)>
^^^^^^^^^^
{
};

without "<Ret(Arg)>", the compiler assumes that you're defining another
*primary class template* with the same name, which violates the ODR.
with "<Ret(Arg)>", it's a partial specialization. Be aware the number if
template parameters in the <must be same as the primary class template.

You can't have a template class with different template arguments
(default arguments also count). So you can't do it.
Quote:
...
>
template <class C>
const std::string& MyClass<C, std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}
>
It would appear to work if I write code for every possible value of C,
but then that's the whole reason for having a template! I'm assuming
I've just gotten the syntax wrong, so if someone would be kind enough to
point out the correct way of doing this it would be much appreciated!
>

--
Thanks
Barry
  #3  
Old September 26th, 2007, 02:55 AM
Adam Nielsen
Guest
 
Posts: n/a
Default Re: Template specialisation with multiple typenames

Hi Barry,

Thanks for your reply!
Quote:
Quote:
>However I then tried to extend the class with a second template
>parameter, and it stopped working:
>>
> template <class C, typename T>
> class MyClass
>
class MyClass<Type1>
>
>
for example:
>
template <class F = void()>
class Func
{
};
>
template <class Ret, class Arg>
class Func<Ret(Arg)>
^^^^^^^^^^
{
};
>
without "<Ret(Arg)>", the compiler assumes that you're defining another
*primary class template* with the same name, which violates the ODR.
with "<Ret(Arg)>", it's a partial specialization. Be aware the number if
template parameters in the <must be same as the primary class template.
Sorry, I don't think I explained myself properly! I'm not after a
second MyClass with different template parameters, I want to remove
MyClass<typename Tand instead have MyClass<class C, typename T>.

This works, but when it comes to defining a function in the class, it
doesn't like my syntax:

// Works with MyClass<typename T(specialisation when T=std::string)
const std::string& MyClass<std::string>::toString() const
{
return this->data;
}

// Does not work with MyClass<class C, typename T>
template <class C>
const std::string& MyClass<C, std::string>::toString() const
{
return this->data;
}

I would like the second function to be used for all instances where T =
std::string, regardless of what C might be given as when constructing a
MyClass object.
Quote:
Quote:
>It would appear to work if I write code for every possible value of C,
>but then that's the whole reason for having a template! I'm assuming
>I've just gotten the syntax wrong, so if someone would be kind enough
>to point out the correct way of doing this it would be much appreciated!
>>
>
>
Thanks,
Adam.
  #4  
Old September 26th, 2007, 08:35 AM
xtrigger303@gmail.com
Guest
 
Posts: n/a
Default Re: Template specialisation with multiple typenames

On Sep 26, 4:44 am, Adam Nielsen <adam.niel...@uq.edu.auwrote:
Quote:
Hi Barry,
>
Thanks for your reply!
>
>
>
Quote:
Quote:
However I then tried to extend the class with a second template
parameter, and it stopped working:
>
Quote:
Quote:
template <class C, typename T>
class MyClass
>
Quote:
class MyClass<Type1>
>
Quote:
for example:
>
Quote:
template <class F = void()>
class Func
{
};
>
Quote:
template <class Ret, class Arg>
class Func<Ret(Arg)>
^^^^^^^^^^
{
};
>
Quote:
without "<Ret(Arg)>", the compiler assumes that you're defining another
*primary class template* with the same name, which violates the ODR.
with "<Ret(Arg)>", it's a partial specialization. Be aware the number if
template parameters in the <must be same as the primary class template.
>
Sorry, I don't think I explained myself properly! I'm not after a
second MyClass with different template parameters, I want to remove
MyClass<typename Tand instead have MyClass<class C, typename T>.
>
This works, but when it comes to defining a function in the class, it
doesn't like my syntax:
>
// Works with MyClass<typename T(specialisation when T=std::string)
const std::string& MyClass<std::string>::toString() const
{
return this->data;
}
>
// Does not work with MyClass<class C, typename T>
template <class C>
const std::string& MyClass<C, std::string>::toString() const
{
return this->data;
}
>
I would like the second function to be used for all instances where T =
std::string, regardless of what C might be given as when constructing a
MyClass object.
>
Quote:
Quote:
It would appear to work if I write code for every possible value of C,
but then that's the whole reason for having a template! I'm assuming
I've just gotten the syntax wrong, so if someone would be kind enough
to point out the correct way of doing this it would be much appreciated!
>
Thanks,
Adam.
Hi,
IMVHO in the case you have described you have to partially specialize
the whole class.
Which is something quite boring. You can you something like the
following trick, obviosuly you have to tailor it to your needs... :-)
Bye,
Francesco

#include <iostream>
#include <string>

template< typename T >
class CTypeFromType
{};

template< typename T1, typename T2 >
class A
{
public:

std::string Do( void ) { return Do( CTypeFromType< T2 >() ); }

private:

template< typename T3 >
std::string Do( CTypeFromType< T3 )
{ return "GENERAL"; }

std::string Do( CTypeFromType< std::string )
{ return "SPECIALIZED"; }
};




int main()
{
A< int, double obj1;
std::cout << obj1.Do() << std::endl;

A< int, std::string obj2;
std::cout << obj2.Do() << std::endl;
}

  #5  
Old September 27th, 2007, 12:25 AM
Adam Nielsen
Guest
 
Posts: n/a
Default Re: Template specialisation with multiple typenames

IMVHO in the case you have described you have to partially specialize
Quote:
the whole class.
Which is something quite boring. You can you something like the
following trick, obviosuly you have to tailor it to your needs... :-)
That's quite an interesting trick, I never thought of doing something
like that - very useful! There's probably a bit too much overhead in
that for my intended use, but it's intriguing nonetheless.

Thanks,
Adam.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.