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

            Bug ID: 90080
           Summary: SFINAE failure with static_cast
           Product: gcc
           Version: 8.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alex at grundis dot de
  Target Milestone: ---

Using `std::enable_if` with a trait checking for `static_castable` fails hard
instead of SFINAE and usage of the alternative signature.

Code (https://godbolt.org/z/2uJc3P):

template<typename T, typename U, typename = U>
struct is_static_castable : std::false_type
{};

template<typename T, typename U>
struct is_static_castable<T, U, decltype(static_cast<U>(std::declval<T>()))> :
std::true_type
{};

template<typename To, typename From, std::enable_if_t<is_static_castable<From*,
To*>::value, int> = 0>
auto safePtrCast(From* from)
{
    return static_cast<To*>(from);
}

template<typename To, typename From,
std::enable_if_t<!is_static_castable<From*, To*>::value, int> = 0>
To* safePtrCast(From* from)
{
    return dynamic_cast<To*>(from);
}

struct BarBase{ virtual ~BarBase() = default;};
struct Bar : virtual BarBase{};

void foo(){
    BarBase* b;
    Bar* b2 = safePtrCast<Bar>(b);
}

Error: 

source>: In instantiation of 'struct is_static_castable<BarBase*, Bar*, Bar*>':

<source>:17:55:   required by substitution of 'template<class To, class From,
typename std::enable_if<(! is_static_castable<From*, To*>::value), int>::type
<anonymous> > To* safePtrCast(From*) [with To = Bar; From = BarBase; typename
std::enable_if<(! is_static_castable<From*, To*>::value), int>::type
<anonymous> = <missing>]'

<source>:28:33:   required from here

<source>:8:42: error: cannot convert from pointer to base class 'BarBase' to
pointer to derived class 'Bar' because the base is virtual

 struct is_static_castable<T, U, decltype(static_cast<U>(std::declval<T>()))> :
std::true_type


This looks like a regression as a similar error is described here
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44267

Reply via email to