473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
6 5654

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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
6
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
11
by: Gernot Frisch | last post by:
// variable creation object template <class T> class CL { public: CL (const T& t) {} template <> CL<double>(const T& t) {} }; How do I make a constructor for T = double, ..., so I can...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think...
1
by: Thomas Barnet-Lamb | last post by:
I have a query concerning the handling of explicit specializations. Essentially, I want to know whether it is legal to take a template like >template<class X> typename X::A Foo(typename X::B,...
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.