So, I answered this question: Define a friends function template for a class template , and I found some "weird" behavior from g ++ (5.3) and clang (3.8)
Assume the following pattern:
template<int M> struct test { private: int value; template<int U, int K> friend test<K> foo (test<U> const t); }; template <int M, int N = 2 * M> test<N> foo (test<M> const t) { test<N> r; r.value = t.value; return r; } int main(){ test<1> t; foo(t); }
Compile both compilers (as expected - if this should not compile, feel free to comment and explain why).
If I changed things to:
template<int U, int K> friend auto foo(test<U> const t); template <int M, int N = 2 * M> auto foo (test<M> const t) { }
This compiler is with g ++, but not with clang, and if I set it to auto and the other to a specific value, for example:
template<int U, int K> friend test<K> foo(test<U> const t); template <int M, int N = 2 * M> auto foo (test<M> const t) { }
Both compilers reject the code saying that:
error: 'int test <2> :: value' is private
My two related questions:
- Which compiler is right for the first case (
auto for declaration / definition)? - Why can't
auto be used when defining a function and test<K> when declaring friendship?
Or in one question: What are the auto rules for declaring friend functions when a function is defined outside the class?
c ++ language-lawyer friend c ++ 14 auto
Holt
source share