inline templated class method as standalone function in namespace

This is a discussion on inline templated class method as standalone function in namespace within the C++ Programming forums, part of the General Programming Boards category; The following test code compiles and executes correctly: Code: class RegExpTest : public Framework { template <class T> int reParseNumbers( ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    inline templated class method as standalone function in namespace

    The following test code compiles and executes correctly:

    Code:
    class RegExpTest : public Framework
    {
       template <class T> int reParseNumbers( std::string, std::set<T>&, double=-1 )
                throw(Exception );
    }
    
    template <class T> int RegExpTest::reParseNumbers( std::string s, std::set<T> & numSet, double set_size_limit )
          throw( Exception )
    {
      // do some regular expression stuff and return an int 
    }
    However when I attempt to incorporate this as a standalone inline function in a namespace (and not as part of a class):

    Code:
    namespace StringFunctions
    {
    
    template <class T>
          inline int reParseNumbers(   std::string s, 
                                        std::set<T> & numSet, 
                                        const double set_size_limit=-1 )
          throw( StringException );
    
    template <class T> 
          inline int reParseNumbers(   std::string s, 
                                        std::set<T>& numSet, 
                                        const double set_size_limit )
          throw( StringException )
          {
           // do some regular expression stuff and return an int 
          }
    }
    I get the following error messages:

    "StringFunctions.hpp", line 5: Error: Templates can only declare classes or functions.
    "StringFunctions.hpp", line 11: Error: Templates can only declare classes or functions.


    I've narrowed the problem with the second code example down to the second std::set<T> parameter that is being passed. Although the code compiles fine as part of my test class, when it is included as a standalone inline'd function in a namespace, the compiler chokes on the second std::set<T> parameter with the error message above.

    What should I do differently to inline a templated method standalone in a namespace (as opposed to having it as part of a class)?


    Any advice would be much appreciated

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The code compiles fine, if <string> and <set> are included and StringException is declared. What compiler are you using?

    By the way, throw specifications are a misfeature and do more harm than good. Get rid of them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Thanks for the response...

    Quote Originally Posted by CornedBee View Post
    The code compiles fine, if <string> and <set> are included and StringException is declared. What compiler are you using?
    CC reports back to me the following version:

    Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25

    I guess I'm off to see if there is any documented compiler bug relevant to this issue...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by CornedBee View Post
    By the way, throw specifications are a misfeature and do more harm than good. Get rid of them.
    Also, in a nutshell, what is the argument against throw(...)?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 08:57 PM
  5. Predeclaration of template class in namespace
    By unregistred in forum C++ Programming
    Replies: 0
    Last Post: 05-30-2003, 04:52 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21