Calling methods of temporary objects created using class template argument deduction

169 views Asked by At

I have the following piece of C++17 code that uses class template deduction:

template <typename T>
struct Test {
        T t;
        Test(T t) : t(t) {}
        bool check() { return true; }
};

template <typename T>
bool check(T t) {
        return Test(t).check();
}

int main() {
        return check(1);
}

gcc 8.2 compiles this without any problems, while clang 7.0 complains:

test.cpp:10:16: error: member reference base type 'Test' is not a structure or union
        return Test(t).check();
               ~~~~~~~^~~~~~

I do not yet have a complete grasp of the intricacies of the class template argument deduction mechanism. Is this a bug in clang or am I using CTAD in a wrong way?

1

There are 1 answers

0
Oliv On BEST ANSWER

This is a clang bug [expr.type.conv]/1:

If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction for the remainder of this subclause.

So template deduction also applies in functional conversion expression.

You can circumvent this clang bug this way:

template <typename T>
    bool check(T t) {
    auto x=Test(t);
    return x.check();
    }