Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling methods of temporary objects created using class template argument deduction [duplicate]

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?

like image 501
Nikola Benes Avatar asked Nov 22 '18 16:11

Nikola Benes


1 Answers

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();
    }
like image 59
Oliv Avatar answered Sep 30 '22 01:09

Oliv