Strange g ++ compiler error when using my own plus () function ("std :: plus is not a function")

I get a strange compiler error from g ++.

It says " std::plus is not a function " for the following code, although I do not include <functional> , and I do not use std where the error occurs.

Here is the code:

 #include <iostream> template<class T1, class T2, class T3> struct MyStruct { T1 t1; T2 t2; T3 t3; MyStruct() {} MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_) : t1(t1_), t2(t2_), t3(t3_) {} }; template<class T1, class T2, class T3> MyStruct<T1, T2, T3> plus(MyStruct<T1, T2, T3> const& x, MyStruct<T1, T2, T3> const& y) { // ... } int main() { typedef MyStruct<int, double, std::string> Struct; Struct x(2, 5.6, "bar"); Struct y(6, 4.1, "foo"); Struct result = plus(x, y); } 

Here is the complete error (slightly reformatted):

 /usr/include/c++/4.2.1/bits/stl_function.h: In function 'int main()': /usr/include/c++/4.2.1/bits/stl_function.h:134: error: 'template<class _Tp> struct std::plus' is not a function, plus3.cc:13: error: conflict with 'template<class T1, class T2, class T3> MyStruct<T1, T2, T3> plus(const MyStruct<T1, T2, T3>&, const MyStruct<T1, T2, T3>&)' plus3.cc:21: error: in call to 'plus' 

Does anyone know why this is so and how to avoid a mistake? I would really like to call the plus function.

The error does not occur if my plus function does not have three template arguments, which makes sense after defining the definition of std::plus :

  template <class _Tp> struct plus : public binary_function<_Tp, _Tp, _Tp> 

But it is still strange, because std::plus should not even be known at this point.

UPDATE:

In response to some answers, I insert a slightly modified code that also gives an error. My plus function is in namespace foo here, and it is called from the same namespace, so there is no need to qualify it with foo::

 #include <string> namespace foo { template<class T1, class T2, class T3> struct MyStruct { T1 t1; T2 t2; T3 t3; MyStruct() {} MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_) : t1(t1_), t2(t2_), t3(t3_) {} }; template<class T1, class T2, class T3> MyStruct<T1, T2, T3> plus(MyStruct<T1, T2, T3> const& x, MyStruct<T1, T2, T3> const& y) { // ... } template<class T1, class T2, class T3> MyStruct<T1, T2, T3> bar(MyStruct<T1, T2, T3> const& x, MyStruct<T1, T2, T3> const& y) { return plus(x, y); } } // end foo int main() { typedef foo::MyStruct<int, double, std::string> Struct; Struct x(2, 5.6, "bar"); Struct y(6, 4.1, "foo"); Struct result = foo::bar(x, y); } 
+4
source share
3 answers

This is because std :: plus is not a function.

Your plus function is in the global namespace, so you need to do

 Struct result = ::plus(x, y); 

The reason he is trying to use std :: plus is because you were not explicitly referenced in the plus version, which you wanted the compiler to use Koenig search , which finds the plus through its arguments.

+5
source

Try specifying a namespace for you plus () explicitly

 Struct result = ::plus(x, y); 

This helps with gcc, and it does not force you to put your plus () in another namespace.

Loki Astari tells the rest

0
source

You will not get this error if you put your function in a different namespace. This is a namespace - you can have the same character / signature in different namespaces without causing collisions.

0
source

Source: https://habr.com/ru/post/1413836/


All Articles