C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to specialize function template when

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Feb 01, 2006 4:00 pm    Post subject: How to specialize function template when Reply with quote



Hi all,

does anyone have an idea how to specialice a function template when the
type does not occur in the parameter list ?

Example:


template <class T> class TheClass
{
private:

std::string a_;
std::string b_;


public:

void dosomething()
{
foo<T>( a_, b_ );
}


};


template<class T> void foo( std::string& a, const std::string& b )
{
// default do nothing
}


template<std::string> void foo( std::string& a, const std::string& b )
{
// do what has to be done for strings
}


Now when i use this code:

TheClass<std::string> theclass;


theclass.dosomething(); // calls default foo not specialized foo


Does anyone know what i am doing wrong ?

Thanks in advance,

Joe


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Tutone
Guest





PostPosted: Wed Feb 01, 2006 6:00 pm    Post subject: Re: How to specialize function template when Reply with quote



fondwey (AT) yahoo (DOT) com wrote:

Quote:
does anyone have an idea how to specialice a function template when the
type does not occur in the parameter list ?

Example:

template <class T> class TheClass
{
private:
std::string a_;
std::string b_;
public:
void dosomething()
{
foo<T>( a_, b_ );
}
};

template<class T> void foo( std::string& a, const std::string& b )
{
// default do nothing
}

template<std::string> void foo( std::string& a, const std::string& b )
{
// do what has to be done for strings
}

Now when i use this code:

TheClass<std::string> theclass;

theclass.dosomething(); // calls default foo not specialized foo

Does anyone know what i am doing wrong ?

You're using the wrong syntax to declare and define the the std::string
specialization of foo(). Try this:

#include <string>
#include <iostream>
#include <ostream>

template<class T>
void foo(std::string& a,
const std::string& b)
{ std::cout << "Default foo()"; }

template<>
void foo<std::string>(std::string& a,
const std::string& b )
{ std::cout << "foo<std::string>()"; }

template <class T> class TheClass {
std::string a_, b_;
public:
void dosomething()
{ foo<T>( a_, b_ ); }
};

int main()
{
TheClass<std::string> theclass;
theclass.dosomething();
}

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Wed Feb 01, 2006 6:00 pm    Post subject: Re: How to specialize function template when Reply with quote



fondwey (AT) yahoo (DOT) com wrote:
Quote:
does anyone have an idea how to specialice a function template when the
type does not occur in the parameter list ?

Example:


template <class T> class TheClass
{
private:

std::string a_;
std::string b_;


public:

void dosomething()
{
foo<T>( a_, b_ );
}


};


template<class T> void foo( std::string& a, const std::string& b )
{
// default do nothing
}


template<std::string> void foo( std::string& a, const std::string& b )

You mean:

template<> void foo<std::string>( ...

don't you?

Quote:
{
// do what has to be done for strings
}


Now when i use this code:

TheClass<std::string> theclass;


theclass.dosomething(); // calls default foo not specialized foo


Does anyone know what i am doing wrong ?

You're using wrong syntax to define a specialisation. Doesn't your
compiler tell you that?

--------------- This code
#include <string>

template<class T> class TheClass
{
std::string a_;
std::string b_;
public:
void dosomething()
{
foo<T>( a_, b_ );
}
};

#include <iostream>

template<class T> void foo( std::string& a, const std::string& b )
{
// default do nothing
std::cout << "default\n";
}

template<> void foo<std::string>( std::string& a, const std::string& b )
{
// do what has to be done for strings
std::cout << "special\n";
}

int main()
{
TheClass<int> tci;
TheClass<std::string> tcs;
tci.dosomething();
tcs.dosomething();
}
--------------- outputs
default
special
------------------------
Apparently, as you intend it to. I didn't test it on all available to me
compilers, but if it doesn't work for you, move the 'foo' definitions
before the 'TheClass' template definition.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Aleš Pergl
Guest





PostPosted: Thu Feb 02, 2006 1:00 am    Post subject: Re: How to specialize function template when Reply with quote

Quote:
Hi all,

does anyone have an idea how to specialice a function template when
the type does not occur in the parameter list ?

Example:

template <class T> class TheClass
{
private:
std::string a_;
std::string b_;
public:

void dosomething()
{
foo<T>( a_, b_ );
}
};

template<class T> void foo( std::string& a, const std::string& b )
{
// default do nothing
}
template<std::string> void foo( std::string& a, const std::string& b )
{
// do what has to be done for strings
}
Now when i use this code:

TheClass<std::string> theclass;

theclass.dosomething(); // calls default foo not specialized foo

Does anyone know what i am doing wrong ?

Thanks in advance,

Joe

You thought you wrote a function template specialization but actually you
wrote another function template whose template argument is a non-type parameter
std::string. The code is legal but means something else than you intended.
The correct syntax for template specialization is this:

template<> void foo<std::string>( std::string& a, const std::string& b ) {}

Regards,
A.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Eugene Alterman
Guest





PostPosted: Thu Feb 02, 2006 12:01 pm    Post subject: Re: How to specialize function template when Reply with quote

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:Xf4Ef.535$a97.432 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net...
Quote:
fondwey (AT) yahoo (DOT) com wrote:

[snip]

Quote:
template<class T> void foo( std::string& a, const std::string& b )
{
// default do nothing
}


template<std::string> void foo( std::string& a, const std::string& b )

You mean:

template<> void foo<std::string>( ...

don't you?

{
// do what has to be done for strings
}


Now when i use this code:

TheClass<std::string> theclass;


theclass.dosomething(); // calls default foo not specialized foo


Does anyone know what i am doing wrong ?

You're using wrong syntax to define a specialisation. Doesn't your
compiler tell you that?

The "specialization" actually declares an unrelated template with a non-type
parameter of std::string type.
Since non-type template parameters of class type are not allowed this
template declaration is ill-formed.

Microsoft VC7.1 compiles it without any diagnostics, but gives a correct
error message if a parameter name is provided:
template<std::string str> void foo( std::string& a, const std::string& b).

It is obviously a compiler bug.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Aleš Pergl
Guest





PostPosted: Thu Feb 02, 2006 6:00 pm    Post subject: Re: How to specialize function template when Reply with quote

Oops, correction... class type 'non-type template parameters' are _not_ legal.
A.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Thu Feb 02, 2006 6:00 pm    Post subject: Re: How to specialize function template when Reply with quote

Thank you all.

You are of course correct, my specialization was completely wrong in
the first place in that it was no specialization at all. Should have
seen that myself, but got the compiler and me sufficiently confused.
What was funny though was, that i had it correct before, but only for
one specialization and not the others. It did somehow not work and
called the default version consistently.

What i also found out is, that when you use the
template-inclusion-model and have the code in a header-file, the
compiler ( at least VS2003 ) complains about multiply-defined symbols.
You need to put the code of the specialized versions into the cpp-file.


I can see more clearly now.

Thanks.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Eugene Alterman
Guest





PostPosted: Thu Feb 02, 2006 9:00 pm    Post subject: Re: How to specialize function template when Reply with quote

<fondwey (AT) yahoo (DOT) com> wrote in message
news:1138897102.432941.124060 (AT) o13g2000cwo (DOT) googlegroups.com...
Quote:
What i also found out is, that when you use the
template-inclusion-model and have the code in a header-file, the
compiler ( at least VS2003 ) complains about multiply-defined symbols.

If you define a function template in a header file you have to declare it
'inline'.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carl Barron
Guest





PostPosted: Sat Feb 04, 2006 2:00 pm    Post subject: Re: How to specialize function template when Reply with quote

Eugene Alterman <eugalt (AT) verizon (DOT) net> wrote:

Quote:
fondwey (AT) yahoo (DOT) com> wrote in message
news:1138897102.432941.124060 (AT) o13g2000cwo (DOT) googlegroups.com...
What i also found out is, that when you use the
template-inclusion-model and have the code in a header-file, the
compiler ( at least VS2003 ) complains about multiply-defined symbols.

If you define a function template in a header file you have to declare it
'inline'.
I don't think so. This is a valid template declaration in a header

template <typename T> void foo(T ) {}

it is up to the compiler/linker to remove duplicate instances if the
code is not inlined.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Martin Eisenberg
Guest





PostPosted: Sat Feb 04, 2006 6:00 pm    Post subject: Re: How to specialize function template when Reply with quote

Eugene Alterman wrote:

Quote:
fondwey (AT) yahoo (DOT) com> wrote in message
news:1138897102.432941.124060 (AT) o13g2000cwo (DOT) googlegroups.com...
What i also found out is, that when you use the
template-inclusion-model and have the code in a header-file,
the compiler ( at least VS2003 ) complains about
multiply-defined symbols.

If you define a function template in a header file you have to
declare it 'inline'.

No, the reason is that a function template specialization is not a
template anymore, it is an ordinary function.


Martin

--
Quidquid latine scriptum sit, altum viditur.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.