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

1

7

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?

Eloisaeloise answered 22/11, 2018 at 16:59 Comment(5)
I can confirm the error, with clang++ -std=c++17.Grevera
Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.Hilmahilt
Yes the issue is clang: godbolt.org/z/bg5cuNMid
How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.Eloisaeloise
The bug report: llvm.org/pr39663Avitzur
M
4

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();
    }
Mid answered 22/11, 2018 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.