#include <iostream>
namespace ns{
const char name[] = "This is a Class Name";//won't compile
//char name[] = "This is a Class Name"; // compiles
template <typename T, char* Name_CA=name>
struct A {
A()
: _data(15)
, _name(Name_CA)
{}
T _data;
char* _name;
};
}
int main()
{
ns::A<int> a;
std::cout << a._name <<"\n";
return 0;
}
In function int main()
error: address of non-extern `ns::name' cannot be used as template
argument
I'm sure the explanation is buried somewhere in Clause 3 esoterica, but can
someone please explain in human readable form, why the version with const
char* won't compile?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell 4 9461
Steven T. Hatton wrote: #include <iostream>
namespace ns{ const char name[] = "This is a Class Name";//won't compile
Make it
extern const char name[] = "This is a Class Name";
//char name[] = "This is a Class Name"; // compiles
template <typename T, char* Name_CA=name> struct A { A() : _data(15) , _name(Name_CA) {} T _data; char* _name; };
}
int main() { ns::A<int> a;
std::cout << a._name <<"\n"; return 0; }
In function int main() error: address of non-extern `ns::name' cannot be used as template argument
I'm sure the explanation is buried somewhere in Clause 3 esoterica, but can someone please explain in human readable form, why the version with const char* won't compile?
Because const char* by default has internal linkage. Non-type template
arguments are required to have external linkage.
Victor
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:KN********************@speakeasy.net... #include <iostream>
namespace ns{ const char name[] = "This is a Class Name";//won't compile //char name[] = "This is a Class Name"; // compiles
template <typename T, char* Name_CA=name> struct A { A() : _data(15) , _name(Name_CA) {} T _data; char* _name; };
}
int main() { ns::A<int> a;
std::cout << a._name <<"\n"; return 0; }
In function int main() error: address of non-extern `ns::name' cannot be used as template argument
I'm sure the explanation is buried somewhere in Clause 3 esoterica, but can someone please explain in human readable form, why the version with const char* won't compile?
Template parameters must have external linkage, const is implicitly internal
linkage in C++. Try
extern const char name[] = "This is a Class Name";
Not sure why template parameters must have external linkage though.
john Template parameters must have external linkage, const is implicitly internal linkage in C++. Try
extern const char name[] = "This is a Class Name";
Not sure why template parameters must have external linkage though.
Imagine you have two source files.
Both of them define the same template (by including its header file).
Both of them have a "const" global variable (which has internal linkage).
They both create a instance of this template (what's the correct lingo
there?) using their global variable.
The problem here is that, because "const" makes it internal linkage, the two
files could have different definitions of a variable of the same name, and
without violating the One Definition Rule. As a result:
Blah<monkey>();
in one file, will not be the same instance of the template as:
Blah<monkey>();
which resides in a different source file.
-JKop
JKop wrote: Template parameters must have external linkage, const is implicitly internal linkage in C++. Try
extern const char name[] = "This is a Class Name";
Not sure why template parameters must have external linkage though.
Imagine you have two source files.
Indeed, that was the other part of the problem. When I made it extern, I
got ODR violations. I ended up doing this:
namespace sth {
namespace tmath {
namespace {
extern const char TensorIndex_CA[] = "TensorIndex";
...
using std::stringstream;
}
template<size_t Order_S, size_t Rank_S>
class TensorIndex : public NamedClass<TensorIndex_CA> {
public:
static const size_t ORDER; // range of indices
static const size_t RANK; // number of indices
static const size_t SIZE; // number of components
static string className();
static string fqClassName();
static string baseClasses();
ostream& print(ostream& out, const size_t& verbosity=0) const;
};
Both of them define the same template (by including its header file).
Both of them have a "const" global variable (which has internal linkage).
At worst it will be namespace local. If I were force to make them global, I
would be switching the C# right now.
They both create a instance of this template (what's the correct lingo there?) using their global variable.
I believe you instantiate a template when you make a type out of it. You
would then instantiate an object of that type before you could use a class
template, etc.
The problem here is that, because "const" makes it internal linkage, the two files could have different definitions of a variable of the same name, and without violating the One Definition Rule. As a result:
Blah<monkey>();
in one file, will not be the same instance of the template as:
Blah<monkey>();
which resides in a different source file.
I'm not sure what to make of that, because it appears I /cannot/ have the
definition visible across translation unit boundaries. So the linkage has
to be extern, but it cannot be externally visible. Go figure.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Robert Allan Schwartz |
last post by:
Why do I get a syntax error below?
I don't see why volatile works but unsigned does not work.
I'm not looking for an answer of the form, "Because the Standard says
so", or "Because the C++...
|
by: Adam |
last post by:
I'm trying to write a template specialization, and the char*
specialization is giving me some trouble. Here's a simplified example:
/**************************/
/* test.cpp */
#include...
|
by: Erik Arner |
last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run
into all sorts of troubles that I'm sure depends on my lack of proper
understanding of C++. I would now like to get it right...
|
by: Vyacheslav Lanovets |
last post by:
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...
|
by: Tony Johansson |
last post by:
Hello!!
I have done some operator overloading but my main testprogram doesn't work
well.
Have you any idea which of my methods are wrong?
#include <iostream>
#include <string>
using...
|
by: uche |
last post by:
can someone please look at this code and decipher it for me....i've
been stressed by it!
=============================================
//disk.cpp
#include "disk.h"
#include <cstdlib>
#include...
|
by: Aries Sun |
last post by:
Hi, guys, recently I read the book "c++ template complete guide",
there is a slice code that I am quite confused. (pasted here). Why
there is an error? Actually I can compile it on Linux, and there...
|
by: Darin Johnson |
last post by:
I keep running across that I'm maintaining that likes to define
function parameters as "const char &" or "const int &", etc.
Ie, constant reference parameters to a primitive type. This is
for...
|
by: cutecutemouse |
last post by:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs;
memset(chs, 0, sizeof(chs));
_snprintf_s(chs, _countof(chs), MAX_STRING_LENGTH, "%f", dbl);
return string...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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:
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
| |