3

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:

  1. Is f1 supposed to compile?
  2. If so, is f2 supposed to compile?
  3. If not, why is there such a difference in handling f1 and f2?

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.

Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
eivour
  • 1,678
  • 12
  • 20
  • 1
    [cppinsights.io](https://cppinsights.io/lnk?code=I2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKdGVtcGxhdGU8Y2xhc3MgVD4KdXNpbmcgRiA9IHN0ZDo6Y29uZGl0aW9uYWxfdDwKICAgICAgICAgICAgc3RkOjppc19zYW1lX3Y8VCwgaW50PiwgaW50LGludChUKT47CnRlbXBsYXRlPGNsYXNzIFQ+CnN0cnVjdCBBewogICAgc3RhdGljIEY8ZG91YmxlPiBmMTsgLy8gT0sKICAgIHN0YXRpYyBGPFQ+IGYyOyAvLyB2YXJpYWJsZSAnZjInIGhhcyBmdW5jdGlvbiB0eXBlCn07CmludCBtYWluKCl7CiAgICBBPGludD4gYTsKICAgIEE8aW50Pjo6ZjEoMS4zKTsKfQ==&insightsOptions=cpp17&std=cpp17&rev=1.0) might provide a hint – Indiana Kernick Jan 17 '20 at 06:32
  • @Kerndog73 ```struct A { static int f1(double); static F f2; }; ``` In ```A```, the type of ```f1``` is instantiated to a function, while the type of ```f2``` remained uninstantiated. (The actual type would be ```int```.) Why is this supposed to happen? – eivour Jan 17 '20 at 06:42
  • 2
    "If a function declaration acquired its function type through a dependent type without using the syntactic form of a function declarator, the program is ill-formed." – Evg Jan 17 '20 at 06:52
  • @Evg Why did the Community user vote to close this? I didn't know it could do that. – Indiana Kernick Jan 17 '20 at 07:17
  • 1
    @Kerndog73, it means that OP accepted the dupe. When somebody suggests a dupe, you'll see "Yes/No" button. – Evg Jan 17 '20 at 07:24

0 Answers0