Thread: What's wrong with the "friend template" snippet

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-13-2002, 12:47 AM
  5. what I'm I doing wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 02-03-2002, 02:46 AM