template function with <char*>

Discussion in 'C++' started by Marc Schellens, Jul 31, 2003.

  1. I have:

    char* byteN = "BYTE";
    char* byte_fun()
    {
    return type_fun< byteN>();
    }

    char* fix_fun()
    {
    return type_fun< "FIX">();
    }


    I the first case, the compiler says:
    basic_fun.cpp:490: `lib::byteN' is not a valid template argument
    basic_fun.cpp:490: it must be the address of an object with external
    linkage
    basic_fun.cpp:490: no matching function for call to `type_fun()'

    but I think it HAS external linkage, hasn't it?

    In the second case the compiler says:
    basic_fun.cpp:494: string literal "FIX" is not a valid template argument
    because it is the address of an object with static linkage

    So why this has to be?

    Any suggestions how to do it?
    thanks,
    marc
     
    Marc Schellens, Jul 31, 2003
    #1
    1. Advertising

  2. "Marc Schellens" <> wrote...
    > I have:
    >
    > char* byteN = "BYTE";


    It's better not to initialise a pointer to non-const char with
    a string literal. While it's allowed for backward compatibility
    reasons (the worst reasons ever), you should avoid those. Use

    char byteN[] = "BYTE";

    or

    const char* byteN = "BYTE";

    > char* byte_fun()
    > {
    > return type_fun< byteN>();


    What's "type_fun"?

    > }
    >
    > char* fix_fun()
    > {
    > return type_fun< "FIX">();
    > }
    >
    >
    > I the first case, the compiler says:
    > basic_fun.cpp:490: `lib::byteN' is not a valid template argument
    > basic_fun.cpp:490: it must be the address of an object with external
    > linkage
    > basic_fun.cpp:490: no matching function for call to `type_fun()'
    >
    > but I think it HAS external linkage, hasn't it?


    That's not the point. It has to be an address (constant thing),
    and you're passing a pointer, the value of which can change during
    run-time.

    > In the second case the compiler says:
    > basic_fun.cpp:494: string literal "FIX" is not a valid template argument
    > because it is the address of an object with static linkage


    [String] literals have no linkage.

    > So why this has to be?


    Why what has to be?

    > Any suggestions how to do it?


    How to do what? Post the definition of the 'type_fun' template,
    otherwise I have to speculate:

    template<char*> char* type_fun() {}

    char somechar;
    char *foo()
    {
    return type_fun<&somechar>();
    }

    What are you trying to accomplish, anyway?

    Victor
     
    Victor Bazarov, Jul 31, 2003
    #2
    1. Advertising

  3. John Harrison wrote:
    > "Marc Schellens" <> wrote in message
    > news:...
    >
    >>I have:
    >>
    >> char* byteN = "BYTE";

    >
    >
    > extern const char byteN[] = "BYTE";
    >
    > and the first example should work. The second never will.
    >
    >
    >> char* byte_fun()
    >> {
    >> return type_fun< byteN>();
    >> }
    >>
    >> char* fix_fun()
    >> {
    >> return type_fun< "FIX">();
    >> }
    >>

    >


    Thanks,
    this works.
    Also without "extern const".
    But it is in fact const. And const alone does not work.
    extern alone gives a warning.
     
    Marc Schellens, Jul 31, 2003
    #3
  4. > "Marc Schellens" <> wrote in message
    > news:...
    >
    >>I have:
    >>
    >> char* byteN = "BYTE";

    >
    >
    > extern const char byteN[] = "BYTE";
    >
    > and the first example should work. The second never will.
    >
    >
    >> char* byte_fun()
    >> {
    >> return type_fun< byteN>();
    >> }
    >>
    >> char* fix_fun()
    >> {
    >> return type_fun< "FIX">();
    >> }
    >>



    BTW: There is no way tp put the definiton of byteN
    inside the function?
     
    Marc Schellens, Jul 31, 2003
    #4
  5. "Marc Schellens" <> wrote...
    > Victor Bazarov wrote:
    > > "Marc Schellens" <> wrote...
    > > [...]
    > >>So why this has to be?

    > >
    > > Why what has to be?

    >
    > Why need template arguments to have external linkage


    So that the same values (addresses) created the same template.
    Otherwise the compiler will try to generate a different function
    and that is not what you want (especially if you happen to have
    a static variable in the body).

    >
    > >
    > >>Any suggestions how to do it?

    > >
    > >
    > > How to do what? Post the definition of the 'type_fun' template,
    > > otherwise I have to speculate:

    >
    > How to pass the template argument.
    >
    > > template<char*> char* type_fun() {}
    > >
    > > char somechar;
    > > char *foo()
    > > {
    > > return type_fun<&somechar>();
    > > }
    > >
    > > What are you trying to accomplish, anyway?

    >
    > Parametrize a function. The function might give out an error.
    > If it will, I want that it reports which instantiation failed.
    >
    > template< class TargetClass, DType targetT, const char* funName>
    > RetType* type_fun();


    Create a bunch of global arrays of char, declare them in the same
    header, include that header in the header in which you have the
    definition of that template function, and use those global arrays
    when instantiating the function.

    If you're trying to create some kind of debug tool, you might be
    better off with a macro, where you could specify a literal...

    Victor
     
    Victor Bazarov, Jul 31, 2003
    #5
  6. "Marc Schellens" <> wrote...
    > > "Marc Schellens" <> wrote in message
    > > news:...
    > >
    > >>I have:
    > >>
    > >> char* byteN = "BYTE";

    > >
    > >
    > > extern const char byteN[] = "BYTE";
    > >
    > > and the first example should work. The second never will.
    > >
    > >
    > >> char* byte_fun()
    > >> {
    > >> return type_fun< byteN>();
    > >> }
    > >>
    > >> char* fix_fun()
    > >> {
    > >> return type_fun< "FIX">();
    > >> }
    > >>

    >
    >
    > BTW: There is no way tp put the definiton of byteN
    > inside the function?


    It won't have linkage if you try that. To have linkage it
    has to be declared/defined in a namespace scope.

    Victor
     
    Victor Bazarov, Jul 31, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    582
  2. wwj
    Replies:
    24
    Views:
    2,550
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    492
    Tristan Miller
    Jan 17, 2004
  4. Replies:
    8
    Views:
    548
    Bryan Donlan
    Aug 26, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,105
    Ian Collins
    May 9, 2006
Loading...

Share This Page