Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:38:00+00:00 2026-05-23T23:38:00+00:00

I know the only way to pass a string literal as template argument is

  • 0

I know the only way to pass a string literal as template argument is to declare it before:

file a.h

#ifndef A_H
#define A_H

#include <string>

char EL[] = "el";


template<char* name>
struct myclass
{
  std::string get_name() { return name; }
};

typedef myclass<EL> myclass_el;

#endif

file a.cpp

#include "a.cpp"

main.cpp

#include "a.h"
...

g++ -c a.cpp
g++ -c main.cpp
g++ -o main main.o a.o

and I got:

a.o:(.data+0x0): multiple definition of `EL'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status

I can’t declare EL as external and I want to keep the a.cpp. Solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:38:01+00:00Added an answer on May 23, 2026 at 11:38 pm

    Let’s start with what the Standard says for the benefit of all, from 14.3.2 Template non-type arguments [temp.arg.nontype] (C++03 Standard):

    1 A template-argument for a non-type, non-template template-parameter
    shall be one of:

    — an integral constant-expression of integral or
    enumeration type; or

    — the name of a non-type template-parameter; or

    — the address of an object or function with external linkage, including
    function templates and function template-ids but excluding non-static
    class members, expressed as & id-expression where the & is optional if
    the name refers to a function or array
    , or if the corresponding
    template-parameter is a reference; or

    — a pointer to member expressed
    as described in 5.3.1 .

    Emphasis mine for the relevant parts.

    Additionally, paragraph 5 lists the conversions that are allowed and one of them is array to pointer decay. Paragraph 2 is even a note that showcases a similar use of char* as that of the OP.

    All that is left is how to have an object in a header with external linkage and no errors. The usual way is a declaration in the header, and one and only one definition in one TU.

    // In header
    extern char EL[]; // array of unspecified size, an incomplete type
                      // extern char EL[3] is acceptable, too.
    
    // In source
    char EL[] = "el";
    

    Note that static is not a possibility because of the requirement that the object have external linkage. The unnamed namespace is to be preferred if the intent is to have a separate object per TU.

    // In header
    // NOT RECOMMENDED! Be wary of ODR-violations with such constructs
    // or simply only use these in source files
    namespace {
    
    // Recommend using const here, which in turn means using extern
    // change non-type template parameter accordingly
    extern const char EL[] = "el";
    
    } // namespace
    

    For the curious, C++0x relaxed the requirement that an object have external linkage to be a valid parameter. (My copy of GCC doesn’t support that yet.) String literals are inexplicably still forbidden to appear as template arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The only way I know of is awkward: 'check for empty return Dim count
The only way I know is: find /home -xdev -samefile file1 But it's really
The only way to install windows-service I know is using Visual Studio 2008 Command
Method chaining is the only way I know to build fluent interfaces. Here's an
Currently, I only know a way of doing RPC for POJOs in Java, and
I only know of one way: Right click and click 'delete me' How else
Is there any way to find out diagonals of quadrilateral if I only know
If there is more than one way, please list them. I only know of
Is there a way to know the main/calling request in an httpmodule? I only
How can I create client proxy if I know only the type of service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.