Creating a template instance using undefined function return types

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().

Is this part of the C ++ standard and can it work on all compilers?

+4
source share
2 answers

Yes, this is part of the C ++ standard.

, , - . (, , , ), .

@dyp, - ([temp.inst]/1), ([dcl.spec.auto]/12).

, . - :

struct Foo {
    //no default constructor
    Foo(int);
};

std::vector<Foo> foos;

std::vector (resize, ) , T , , std::vector.

+11

, . Wrapper<T>::call_f() , .

$14.7.1/2 [Temp.inst]:

- , , , ,

$14.7.1/8 [Temp.inst]:

, , -, -, -, constexpr if ( [stmt.if]), .

+2

Source: https://habr.com/ru/post/1650510/


All Articles