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 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?
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
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
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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. ...
|
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:
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
| |