OiO.lk Blog templates Template deduction guide doesn't work well with aggregate initialization
templates

Template deduction guide doesn't work well with aggregate initialization


I am studying how to use deduction guides. I composed this program.

template<typename T>
struct Test {
    void f() {
        cout << std::is_same_v<T, std::size_t> << endl;
    }
    T t;
};

Test(int) -> Test<std::size_t>;

int main() {
    Test t1(1.2);
    t1.f();
}

I expected the deduction guide will only kick in when if I pass an integer to the aggregate initialization. Since I pass 1.2, I expected T will be deducted to double which won’t triggered the deduction guide. However the output is 1, i.e. T -> int -> size_t.

I tried to add a constructor to the struct.

template<typename T>
struct Test {
    void f() {
        cout << std::is_same_v<T, std::size_t> << endl;
    }
    Test(T v): t(v) {}
    T t;
};

Test(int) -> Test<std::size_t>;

int main() {
    Test t1(1.2);
    t1.f();
}

The output was 0 which matched my previous expectation. Comparing these two programs, the only difference here is how the struct got initialized. What does the deduction guide not work well with the aggregate initialization? Thanks.



You need to sign in to view this answers

Exit mobile version