struct Value { using a_type = int; a_type f() { return 1; } }; template<typename T> struct Wrapper { T t; auto call_f() { return t.f(); } }; int main() { Wrapper<Value> w; Wrapper<int> w2; w.call_f(); }
This compiles on Clang and GCC. Wrapper<int>gets an instance, even if the return type Wrapper<int>::call_f()cannot be inferred (no int::f()). It fails only when called w2.call_f().
Wrapper<int>
Wrapper<int>::call_f()
int::f()
w2.call_f()
Is this part of the C ++ standard and can it work on all compilers?
Yes, this is part of the C ++ standard.
, , - . (, , , ), .
@dyp, - ([temp.inst]/1), ([dcl.spec.auto]/12).
[temp.inst]/1
[dcl.spec.auto]/12
, . - :
struct Foo { //no default constructor Foo(int); }; std::vector<Foo> foos;
std::vector (resize, ) , T , , std::vector.
std::vector
resize
T
, . Wrapper<T>::call_f() , .
Wrapper<T>::call_f()
$14.7.1/2 [Temp.inst]:
- , , , ,
$14.7.1/8 [Temp.inst]:
, , -, -, -, constexpr if ( [stmt.if]), .
Source: https://habr.com/ru/post/1650510/More articles:How to break a line with multiple regexp definition in clojure? - stringSymfony2: raw form data access in validator - phpSpark writes data to a partitioned Hive table very slowly - hadoopHow to convert LocalDateTime to GregorianCalendar? - javaFailed to get ZonedDateTime from TemporalAccessor while parsing date - javaSitecore: set relative data source when using branch - sitecoreHow to unit test code wrapped in `runOnUiThread`? - androidApple Application Error - I Can't Play It - iosОтсутствует скины в выходном файле? - flexApp get Rejected: Crash on startup, but works fine for me and on all devices - objective-cAll Articles