Template Parameter List non-type

K

Kyle Kolander

I would like one of my template parameters to be an std::string.
I realize this is a non-type and non-integral value.
What options are available to me?

template <class T, string S> // not allowed
class A {...};

I can get this to compile as follows:

template <class T, const string& S>
class A {...};
string s("some string");
A<int, s> a;

However, if my definition of s is:

const string s("some string");

It will not compile... unfortunately, I am working with a lot of previously
defined const strings.

I imagine there is an elegant solution to this problem... Anyone have a
suggestion/solution?

Thanks,
Kyle
 
V

Victor Bazarov

Kyle said:
I would like one of my template parameters to be an std::string.
I realize this is a non-type and non-integral value.
What options are available to me?

template <class T, string S> // not allowed

It's not allowed because rvalues have no linkage. Non-type template
arguments need to have external linkage if they are not integral values.
class A {...};

I can get this to compile as follows:

template <class T, const string& S>
class A {...};
string s("some string");
A<int, s> a;

However, if my definition of s is:

const string s("some string");

.... which makes 's' to have internal linkage ...
It will not compile... unfortunately, I am working with a lot of previously
defined const strings.

Declare them 'extern'.
I imagine there is an elegant solution to this problem... Anyone have a
suggestion/solution?

See above. And get a good book on templates.

V
 
K

Kyle Kolander

Thanks Victor!

Your suggestion regarding extern solves my problem.
However, there appears to be one small issue I would like to avoid.
As I mentioned earlier, the const strings that I have to work with are
previously defined in a header file.
In order to get this working, I have to modify their definitions to be
extern as follows:

// defs.h
const std::string str = "some string"; // as it currently exists (this
will not work)

I can change it to this:
extern const std::string str = "some string"; // now it will work

I understand that consts have internal linkage by default and that extern
will give it external linkage.
So, I can make this change to the const strings... but would like to not
modify the existing code.
Is there any way around this? Thanks again for your help.

Here is a complete contrived example:

// defs.h
#ifndef Defs_h
#define Defs_h

extern const std::string str = "some string";

#endif


// temp.h
#ifndef Temp_h
#define Temp_h

#include <iostream>
#include <string>
#include "defs.h"

template <class T, const std::string& S>
class A
{
public:
A() {std::cout << S << std::endl;}
};

typedef A<int,str> AType;

#endif


// main.cpp
#include "temp.h"

int main(int argc, char* argv[])
{
AType at;
return 0;
}


Thanks,
Kyle
 
V

Victor Bazarov

Kyle said:
Your suggestion regarding extern solves my problem.
However, there appears to be one small issue I would like to avoid.
[...]
So, I can make this change to the const strings... but would like to not
modify the existing code.
Is there any way around this?[...]

Let me get this straight... You want to fix your problem but you don't
want to modify any code. Did I get it right? If so, I do not believe it
can be done. You see, it is *often* a necessity that the code has to
change (sometimes very little) in order to accommodate the fix. It's not
*always* so, but *often*. In your case, there is no way around it.
 
K

Kyle Kolander

Thanks for your help, Victor.
You are correct in your understanding...
I was hoping for a way to not have to modify our existing const string
definitions.
Looks like I'll have to anyhow. Not a big deal. Thanks again.
I appreciate your help and patience.

Kyle

Victor Bazarov said:
Kyle said:
Your suggestion regarding extern solves my problem.
However, there appears to be one small issue I would like to avoid.
[...]
So, I can make this change to the const strings... but would like to not
modify the existing code.
Is there any way around this?[...]

Let me get this straight... You want to fix your problem but you don't
want to modify any code. Did I get it right? If so, I do not believe it
can be done. You see, it is *often* a necessity that the code has to
change (sometimes very little) in order to accommodate the fix. It's not
*always* so, but *often*. In your case, there is no way around it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top