llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.03k stars 11.58k forks source link

Unintuitive behavior for deduction guides for constrained template classes #54310

Open jgopel opened 2 years ago

jgopel commented 2 years ago

Given a template class whose class type is constrained by a concept, if the deduction guide is written without that same constraint it will never be selected because it is always less specific. I suggest that this behavior is subtle and unexpected and should produce a warning.

Sample code:

template <typename T>
concept C = true;

void foo() noexcept;
void bar() noexcept;

template <C T>
struct S {
    S() noexcept = default;
    S(const S&) noexcept { foo(); }
    S(const T&) noexcept { bar(); }
};

// Deduction guide with unexpected behavior
template <typename T>
S(S<T>) -> S<S<T>>;

// // Deduction guide with expected behavior
// template <C T>
// S(S<T>) -> S<S<T>>;

https://godbolt.org/z/T79f164nW

jgopel commented 2 years ago

Link to GCC bug for for selecting the less constrained result and request for a warning: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104873