template function with <char*>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Schellens

    template function with <char*>

    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:4 90: `lib::byteN' is not a valid template argument
    basic_fun.cpp:4 90: it must be the address of an object with external
    linkage
    basic_fun.cpp:4 90: 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:4 94: 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

  • Victor Bazarov

    #2
    Re: template function with &lt;char*&gt ;

    "Marc Schellens" <m_schellens@ho tmail.com> wrote...[color=blue]
    > I have:
    >
    > char* byteN = "BYTE";[/color]

    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";
    [color=blue]
    > char* byte_fun()
    > {
    > return type_fun< byteN>();[/color]

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

    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.
    [color=blue]
    > In the second case the compiler says:
    > basic_fun.cpp:4 94: string literal "FIX" is not a valid template argument
    > because it is the address of an object with static linkage[/color]

    [String] literals have no linkage.
    [color=blue]
    > So why this has to be?[/color]

    Why what has to be?
    [color=blue]
    > Any suggestions how to do it?[/color]

    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<&somec har>();
    }

    What are you trying to accomplish, anyway?

    Victor


    Comment

    • Marc Schellens

      #3
      Re: template function with &lt;char*&gt ;



      John Harrison wrote:[color=blue]
      > "Marc Schellens" <m_schellens@ho tmail.com> wrote in message
      > news:3F292115.8 020005@hotmail. com...
      >[color=green]
      >>I have:
      >>
      >> char* byteN = "BYTE";[/color]
      >
      >
      > extern const char byteN[] = "BYTE";
      >
      > and the first example should work. The second never will.
      >
      >[color=green]
      >> char* byte_fun()
      >> {
      >> return type_fun< byteN>();
      >> }
      >>
      >> char* fix_fun()
      >> {
      >> return type_fun< "FIX">();
      >> }
      >>[/color]
      >[/color]

      Thanks,
      this works.
      Also without "extern const".
      But it is in fact const. And const alone does not work.
      extern alone gives a warning.

      Comment

      • Marc Schellens

        #4
        Re: template function with &lt;char*&gt ;

        > "Marc Schellens" <m_schellens@ho tmail.com> wrote in message[color=blue]
        > news:3F292115.8 020005@hotmail. com...
        >[color=green]
        >>I have:
        >>
        >> char* byteN = "BYTE";[/color]
        >
        >
        > extern const char byteN[] = "BYTE";
        >
        > and the first example should work. The second never will.
        >
        >[color=green]
        >> char* byte_fun()
        >> {
        >> return type_fun< byteN>();
        >> }
        >>
        >> char* fix_fun()
        >> {
        >> return type_fun< "FIX">();
        >> }
        >>[/color][/color]


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

        Comment

        • Victor Bazarov

          #5
          Re: template function with &lt;char*&gt ;

          "Marc Schellens" <m_schellens@ho tmail.com> wrote...[color=blue]
          > Victor Bazarov wrote:[color=green]
          > > "Marc Schellens" <m_schellens@ho tmail.com> wrote...
          > > [...][color=darkred]
          > >>So why this has to be?[/color]
          > >
          > > Why what has to be?[/color]
          >
          > Why need template arguments to have external linkage[/color]

          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).
          [color=blue]
          >[color=green]
          > >[color=darkred]
          > >>Any suggestions how to do it?[/color]
          > >
          > >
          > > How to do what? Post the definition of the 'type_fun' template,
          > > otherwise I have to speculate:[/color]
          >
          > How to pass the template argument.
          >[color=green]
          > > template<char*> char* type_fun() {}
          > >
          > > char somechar;
          > > char *foo()
          > > {
          > > return type_fun<&somec har>();
          > > }
          > >
          > > What are you trying to accomplish, anyway?[/color]
          >
          > 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();[/color]

          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


          Comment

          • Victor Bazarov

            #6
            Re: template function with &lt;char*&gt ;

            "Marc Schellens" <m_schellens@ho tmail.com> wrote...[color=blue][color=green]
            > > "Marc Schellens" <m_schellens@ho tmail.com> wrote in message
            > > news:3F292115.8 020005@hotmail. com...
            > >[color=darkred]
            > >>I have:
            > >>
            > >> char* byteN = "BYTE";[/color]
            > >
            > >
            > > extern const char byteN[] = "BYTE";
            > >
            > > and the first example should work. The second never will.
            > >
            > >[color=darkred]
            > >> char* byte_fun()
            > >> {
            > >> return type_fun< byteN>();
            > >> }
            > >>
            > >> char* fix_fun()
            > >> {
            > >> return type_fun< "FIX">();
            > >> }
            > >>[/color][/color]
            >
            >
            > BTW: There is no way tp put the definiton of byteN
            > inside the function?[/color]

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

            Victor


            Comment

            Working...