#include<iostream>template<typenameT>classFoo;template<typenameT>booloperator==(constFoo<T>&,constFoo<T>&);template<typenameT>booloperator!=(constFoo<T>&,constFoo<T>&);template<typenameT>classFoo{public:Foo(){}//--------------------------------------------------------------------------//--------------------------------------------------------------------------// Leaving off <> makes CPPCHECK happy but generates a G++ warningfriendbooloperator==(constFoo<T>&,constFoo<T>&);//// main.cpp:24:65: warning: friend declaration 'bool operator==(const Foo<T>&, const Foo<T>&)' declares a non-template function [-Wnon-template-friend]// friend bool operator== (const Foo<T> &, const Foo<T> &);// ^// main.cpp:24:65:note:(if this is not what you intended, make sure// the function template has already been declared and add <> after the// function name here)//--------------------------------------------------------------------------//--------------------------------------------------------------------------// This fails CPPCHECK - but builds w/o warning on gcc 5.2 (C++11)// I believe this is the correct syntax for a non-inline friend template function.// See: Template friend operators// https://en.cppreference.com/w/cpp/language/friendfriendbooloperator!=<>(constFoo<T>&,constFoo<T>&);private:Timpl_{0};};template<typenameT>booloperator==(constFoo<T>&a,constFoo<T>&b){std::cout<<"hello ==\n";returna.impl_==b.impl_;}template<typenameT>booloperator!=(constFoo<T>&a,constFoo<T>&b){std::cout<<"hello !=\n";returna.impl_==b.impl_;}intmain(){Foo<int>v1;Foo<int>v2;v1==v2;v1!=v2;}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Snippet compiler: https://coliru.stacked-crooked.com/view?id=1a00c4e44f0c7506
Thanks! I can reproduce. I created this ticket: https://trac.cppcheck.net/ticket/9771