423,109 Members | 2,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,109 IT Pros & Developers. It's quick & easy.

Why does Explicit specialization of function templates cause generation of code?

P: n/a
Hello, All!

I know that Explicit Instantiation actually emits code to obj files (so you
can even export them from the module as plain functions or classes).

But I found that MSVC7.1 compiler does the same in case of Explicit
Specialization, so I either have to delcare specializations inline or move
definitions to cpp file to avoid LNK2005 ("already defined") errors.

Why is that?

Best regards, Vyacheslav Lanovets
Aug 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Vyacheslav Lanovets wrote:
Hello, All!

I know that Explicit Instantiation actually emits code to obj files (so you
can even export them from the module as plain functions or classes).

But I found that MSVC7.1 compiler does the same in case of Explicit
Specialization, so I either have to delcare specializations inline or move
definitions to cpp file to avoid LNK2005 ("already defined") errors.

Why is that?


Could you post an example?

Aug 23 '05 #2

P: n/a
Hello, Maxim!
You wrote on 23 Aug 2005 01:07:56 -0700:

MY>> But I found that MSVC7.1 compiler does the same in case of Explicit
MY>> Specialization, so I either have to delcare specializations inline or
MY>> move definitions to cpp file to avoid LNK2005 ("already defined")
MY>> errors.
MY>>
MY>> Why is that?

MY> Could you post an example?
Piece of cake :)

=== x.h ===

#pragma once

template <class TTYPE>
bool MakeValue(const char* strValue, TTYPE value)
{
return true;
}

template <>
bool MakeValue(const char* strValue, int value)
{
return true;
}

template <>
bool MakeValue(const char* strValue, double value)
{
return true;
}

=== f1.cpp ===
#include "x.h"

=== f2.cpp ===
#include "x.h"

....
Linking...

f2.obj : error LNK2005: "bool __cdecl MakeValue<int>(char const *,int)"
(??$MakeValue@H@@YA_NPBDH@Z) already defined in f1.obj

f2.obj : error LNK2005: "bool __cdecl MakeValue<double>(char const
*,double)" (??$MakeValue@N@@YA_NPBDN@Z) already defined in f1.obj

Debug/testTemplSpec.exe : fatal error LNK1169: one or more multiply defined
symbols found

Best regards, Vyacheslav Lanovets
Aug 23 '05 #3

P: n/a
Vyacheslav Lanovets wrote:
template <>
bool MakeValue(const char* strValue, int value)


dunno what that is, but it's not a full specialisation.
This is:
template <> bool MakeValue<int>(const char *, int value)
Note the <int>.

Marc
Aug 23 '05 #4

P: n/a

Vyacheslav Lanovets wrote:
Hello, Maxim!
You wrote on 23 Aug 2005 01:07:56 -0700:

MY>> But I found that MSVC7.1 compiler does the same in case of Explicit
MY>> Specialization, so I either have to delcare specializations inline or
MY>> move definitions to cpp file to avoid LNK2005 ("already defined")
MY>> errors.
MY>>
MY>> Why is that?

MY> Could you post an example?
Piece of cake :)

=== x.h ===

#pragma once

template <class TTYPE>
bool MakeValue(const char* strValue, TTYPE value)
{
return true;
}

template <>
bool MakeValue(const char* strValue, int value)
{
return true;
}

template <>
bool MakeValue(const char* strValue, double value)
{
return true;
}

=== f1.cpp ===
#include "x.h"

=== f2.cpp ===
#include "x.h"

...
Linking...

f2.obj : error LNK2005: "bool __cdecl MakeValue<int>(char const *,int)"
(??$MakeValue@H@@YA_NPBDH@Z) already defined in f1.obj

f2.obj : error LNK2005: "bool __cdecl MakeValue<double>(char const
*,double)" (??$MakeValue@N@@YA_NPBDN@Z) already defined in f1.obj

Debug/testTemplSpec.exe : fatal error LNK1169: one or more multiply defined
symbols found


Function templates are an exempt of ODR and may be more than one
definition of them in different TU's. Full function template
specialization is not a template, rather an ordinary function, so you
need to use inline keyword not to violate ODR if you want to put them
in a header file included into several TU's.

Aug 23 '05 #5

P: n/a
Hello, Marc!
You wrote on Tue, 23 Aug 2005 15:12:58 +0200:

MM> Vyacheslav Lanovets wrote:
MM>> template <>
MM>> bool MakeValue(const char* strValue, int value)

MM> dunno what that is, but it's not a full specialisation.
MM> This is:
MM> template <> bool MakeValue<int>(const char *, int value)
MM> Note the <int>.

Believe it or not, but omitting template arguments for specialization is ok:

14.7.3.11. A trailing template-argument can be left unspecified in the
template-id naming an explicit function template specialization provided it
can be deduced from the function argument type.

And VC++ claims to support this since version 5.0

Regards, Vyacheslav Lanovets
Aug 24 '05 #6

P: n/a
Vyacheslav Lanovets wrote:
<snip>
Believe it or not, but omitting template arguments for
specialization is ok:

14.7.3.11.*A*trailing*template-argument*can*be*left
unspecified*in*the template-id naming an explicit
function template specialization provided it can be
deduced from the function argument type.

<snip>

:o

How useless :)

Thanks,
Marc

Aug 24 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.