Thread: specialized template function results in duplicate symbols

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    specialized template function results in duplicate symbols

    Hi all,

    as this is my first post to this board, I hope, I do not repeat a FAQ here. Anyhow, I was searching for my problem in the forum without success. Hence, I'd like to apologize in advance, if this question has been asked too often, already

    But here's my problem: I have a helper method for string to any, using a stringstream. Unfortunately, the stringstream stops at white spaces and hence, I would like to specialize the function for strings:

    Code:
    // template to convert a string to any type supported by stringstream
    template <class type>
    bool string2any( const std::string input, type &output )
    {
      // use a stringstream to convert the string to a number
      std::stringstream converter(input);
      converter >> output;
    
      if( converter.fail() )
        return false;
      return true;
    }
    
    // specialized template from above - for both std::strings, to support strings with whitespaces
    template <>
    bool string2any<std::string>( const std::string input, std::string &output )
    {
      output = input;
      return true;
    }
    If I use this template in two cpp files and link them together, I get the following error:
    Code:
    ld: duplicate symbol bool string2any<std::basic_string<char, std::char_traits<char>, 
    std::allocator<char> > >(std::basic_string<char, std::char_traits<char>, 
    std::allocator<char> >, std::basic_string<char, std::char_traits<char>, 
    std::allocator<char> >&)in /var/folders/GL/GLgvvL6NETK1iaM7tYVFwk+++TI/-Tmp-
    //ccKgJ9C0.o and /var/folders/GL/GLgvvL6NETK1iaM7tYVFwk+++TI/-Tmp-//cccZPuby.o
    collect2: ld returned 1 exit status
    I wonder, why the compiler creates multiple objects for it and what template function specialization is for, if such things fail...?

    Thanks in advance!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it's in a header, try the inline keyword which instructs the linker that multiple definitions are OK. (Template functions are implicitly inline, but I suppose this doesn't apply to specializations.)

    Next, it doesn't have to be a specialization. A non-template overload would be simpler (still declare inline, or define in a source file).

    You probably also wanted to pass the input by const reference.

    And lastly, this functionality is also readily available in the boost library (lexical_cast).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Thanks, it can be so easy :/

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The function specialization results in the same function signature as the template. Do not specialize template functions, overload them instead.

    Code:
    bool string2any( const std::string input, std::string &output )
    {
      output = input;
      return true;
    }
    (And it's wasteful to pass the input argument by value. You should consider using a const reference)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread