Bogus error when using __reference_converts_from_temporary in default template argument · Issue #132044 · llvm/llvm-project (original) (raw)

We ran into this with an std::function (libstdc++ 13) returning an object with protected copy constructor. It boils down to this:

class X { protected: X(const X&); };

static_assert(!__reference_converts_from_temporary(X, X));

template<typename A, typename B, bool Dangle = __reference_converts_from_temporary(A, B)> static void test();

using Result = decltype(test<X, X>());

produces:

bug.cpp:9:24: error: calling a protected constructor of class 'X'
    9 |          bool Dangle = __reference_converts_from_temporary(A, B)>
      |                        ^

Replacing either A or B by X doesn't make the error go away. Interestingly, if we define test, for example with an empty body, and do a full instantiation, then the error appears if the instantiation is implicit, but not if it's explicit:

template<typename A, typename B, bool Dangle = __reference_converts_from_temporary(A, B)> static void test() {} // <-- Added an empty body.

template void test<X, X>(); // No error.

void f() { test<X, X>(); // Error. }