https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95066

            Bug ID: 95066
           Summary: [C++ 20] Incorrect valid compilation with a
                    conditional explicit
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ojman101 at protonmail dot com
  Target Milestone: ---

The code below is invalid C++, the line "Foo<int> b = a;" should fail to
compile as implicitly casting is made illegal by the conditional explicit using
the "IsSafelyCastable" predicate.

----------------------------------------------------------------
#include <type_traits>

template <typename, typename>
class IsSafelyCastable : public std::false_type {};

template <>
class IsSafelyCastable<int, float> : public std::true_type {};

template <typename T>
struct Foo {
    template <typename U>
    explicit(!IsSafelyCastable<T, U>::value) operator Foo<U>();
};

template <typename T>
template <typename U>
Foo<T>::operator Foo<U>() {
  return {};
}

int main() {
    Foo<float> a;
    Foo<int> b = a;
}
----------------------------------------------------------------

Clang 10 correctly evaluates the explicit condition to be true and blocks the
implicit cast. However, GCC 9.3.0 successfully compiles without any errors. I
believe this to be a GCC bug as subtle changes can make GCC produce the correct
error. For example, moving the definition of the function to be inline with the
declaration.

Reply via email to