I have the following code, which does not compile with both g++ 8.3 and clang 9.0.0 with -std=c++2a
.
#include <type_traits>
template<class T>
using F = std::conditional_t<
std::is_same_v<T, int>, int,int(T)>;
template<class T>
struct A{
static F<double> f1; // OK
static F<T> f2; // variable 'f2' has function type
};
int main(){
A<double> a;
A<double>::f1(1.3);
}
If I comment out this line
static F<T> f2; // variable 'f2' has function type
it will compile (with a link error reporting undefined reference to A<double>::f1(double)
).
Here are the questions:
- Is
f1
supposed to compile? - If so, is
f2
supposed to compile? - If not, why is there such a difference in handling
f1
andf2
?
It seems that it compiles if and only if the compilers know the type F<T>
is a function "beforehand", but I am not sure if that is the correct behavior.