Advertisement

05.13.2004 at 02:33AM PDT, ID: 20987840
[x]
Attachment Details

template class with character constant arguments

Asked by stefan73 in C++ Programming Language

Tags: template, argument, string

How can I create a template instance which has a char* as argument?

I tried:

template <const char* const N1,typename T1>
class name_value_pair{
      T1& val;
      name_value_pair(T1& valx) : val(valx) {}
      print(){
            cout << N1 << "=" << val << endl;
      }
};

int main(){
      string alpha="123";

      name_value_pair<"alpha",typeof(alpha)> x(alpha);

      x.print();

      return 0;
}

...but this results in (g++):
string_templates.cpp:18: error: string literal "alpha" is not a valid template argument because it is the address of an object with static linkage

Using const char[] instead doesn't work - same error.

Ideas, anyone?

StefanStart Free Trial
 
Loading Advertisement...
 
[+][-]05.13.2004 at 02:51AM PDT, ID: 11057517

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.13.2004 at 02:52AM PDT, ID: 11057527

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Tags: template, argument, string
Sign Up Now!
Solution Provided By: Sys_Prog
Participating Experts: 3
Solution Grade: A
 
 
[+][-]05.13.2004 at 07:54AM PDT, ID: 11059738

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2004 at 07:55AM PDT, ID: 11059761

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2004 at 08:24AM PDT, ID: 11060128

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2004 at 09:31AM PDT, ID: 11060802

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.14.2004 at 12:22AM PDT, ID: 11065924

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.14.2004 at 12:23AM PDT, ID: 11065926

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32