What's wrong with the "friend template" snippet

This is a discussion on What's wrong with the "friend template" snippet within the C++ Programming forums, part of the General Programming Boards category; I am following the http://www.parashift.com/c++-faq-lit...html#faq-35.16 to write the snippet below: Code: template<typename T> class Foo { public: Foo(const T& value ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What's wrong with the "friend template" snippet

    I am following the http://www.parashift.com/c++-faq-lit...html#faq-35.16 to write the snippet below:

    Code:
    template<typename T>
    class Foo {
    public:
        Foo(const T& value = T());
    
        friend T bar (T x){
            return x;
        }
    
    private:
        T value_;
    };
    
    int main(){
        Foo<int> obj;
        bar<int>(3); //correct if I comment this line
    
        return 1;
    }
    According to the link, if I define the friend function body within the class, complier won't complain anything.
    It IS true if I don't call
    Code:
     bar<int>(3);
    But if I do call the function it complains:
    g++ -c -g main.cc -o main.o
    main.cc: In function `int main()':
    main.cc:17: error: parse error before `>' token
    make: *** [main.o] Error 1
    So what is the correct way to call bar?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by V
    So what is the correct way to call bar?
    bar(3) works or me, but frankly, I cannot explain exactly what's happening.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    According to the FAQ there are two ways, define bar inline or define it outside the class.

    It appears that if you define it outside as a template function as shown in the example, bar is template function.

    If defined inline, the compiler generates a non-template function for each instantiation of Foo with a different type - here int bar(int) - and there still won't be linker errors because the function exists for this type of Foo, except bar won't be a template function and you can't use it with the template syntax.
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  2. What am I doing wrong?
    By WanderinWeezard in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2002, 09:53 AM
  3. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM
  4. Perhaps someone can see where i am wrong?
    By laree in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 11:47 PM
  5. what I'm I doing wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 02-03-2002, 01:46 AM

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