Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct syntax for friend template function

In The C++ Programming Language, Fourth Edition - chapter 23.4.7 Friends, I found following example (I have slightly modified it to show only relevant part):

template<typename T>
class Vector {
public:
    friend Vector operator*<>(const Vector& v, int f); 
                           ^^ ~~~~ ?
};

template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
    return v;
}

I tried to compile it, but I get following error (clang):

main.cpp:8:20: error: friends can only be classes or functions
        friend Vector operator*<>(const Vector& v, int f); 
                      ^
main.cpp:8:29: error: expected ';' at end of declaration list
        friend Vector operator*<>(const Vector& v, int f); 
                               ^
                               ;
2 errors generated.

Book explains that :

The <> after the name of the friend function is needed to make clear that the friend is a template function. Without the <>, a non template function would be assumed.

And that is all on this.

Without <> this code compiles, but when operator* is used (ex.: Vector<int> v; v*12;) then linker error appears:

main.cpp:(.text+0xb): undefined reference to `operator*(Vector<int> const&, int)'

So I assume that <> is needed to tell compiler that function template for operator* should be generated each time Vector template is instantiated for given type.

But what am I doing wrong in the example from the book, and why?

like image 733
mike Avatar asked Mar 09 '17 09:03

mike


People also ask

What is the syntax of friends function?

Syntax of friend functions:class className{ // Other Declarations friend returnType functionName(arg list); }; As we can see above, the friend function should be declared inside the class whose private and protected members are to be accessed.

What is the correct syntax of defining function template template functions?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

What is the syntax of friend function friend class?

Syntax : class <class_name> { friend <return_type> <function_name>(argument/s); }; C++

Which is correct syntax to define template?

What is the correct syntax of defining function template/template functions? Explanation: Starts with keyword template and then <class VAR>, then use VAR as type anywhere in the function below.


2 Answers

As the book said,

the <> after the name of the friend function is needed to make clear that the friend is a template function.

That means, the name should refer to a function template, which should be declared (as template) in advance. e.g.

// forward declaration of the class template
template<typename T>
class Vector;

// declaration of the function template
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f);

template<typename T>
class Vector {
public:
    // friend declaration
    friend Vector operator*<>(const Vector& v, int f); 
};

// definition of the function template
template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
    return v;
}
like image 84
songyuanyao Avatar answered Nov 06 '22 10:11

songyuanyao


In your case, you're declaring operator* as a friend directly inside Vector, without any previous declaration. Therefore the correct syntax is:

template<typename T>
class Vector {
public:
    template<typename>
    friend Vector operator*(const Vector& v, int f);               
};

template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
    return v;
}

live example on wandbox

like image 26
Vittorio Romeo Avatar answered Nov 06 '22 11:11

Vittorio Romeo