#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 9486
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******@setid ava.kushan.aa> wrote in message
news:KN******** ************@sp eakeasy.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[] = "TensorInde x";
...
using std::stringstre am;
}
template<size_t Order_S, size_t Rank_S>
class TensorIndex : public NamedClass<Tens orIndex_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++ grammer says so"; I'm looking for an
explanation of *why* the Standard and/or the grammar say so.
Thanks,
|
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 "try.hpp"
int main(){}
/**************************/
|
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 once and for all,
if possible.
Most severe is the problem illustrated by the code below. It's based on
the "pluggable factory pattern" described in
http://www.adtmag.com/joop/crarticle.asp?ID=1520
|
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 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?
|
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 namespace std;
| |
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 <iomanip>
#include <iostream>
#include <fstream>
|
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 is
only an warning.
Thanks!!!
#include <iostream>
#include <cstring>
#include <string>
|
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 normal functions, not operators.
I keep changing these to just have the plain old type, which
is more efficient (I'm using embedded systems) and less
obtuse. I'm puzzled why this one programmer insisted on
odd style everywhere. Maybe he's just...
|
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 (chs);
}
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |