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

            Bug ID: 96746
           Summary: Type Casting in template function should not be
                    type-dependent if the type of the conversion result is
                    not type-dependent.
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: masamitsu.murase at gmail dot com
  Target Milestone: ---

The following code compiles fine with GCC 11.0, but I think that it should not
compile
because of the mismatch of the argument type of "func_with_int_arg" at (*1) .
    #include <type_traits>

    void func_with_int_arg(int) {}

    template<typename T>
    std::enable_if_t<std::is_same<T, int*>::value>
    func(T arg)
    {
        func_with_int_arg(static_cast<int*>(arg));  // (*1)
    }

    template<typename T>
    std::enable_if_t<std::is_same<T, int>::value>
    func(T) {}

    int main(int, char **)
    {
        func(1);  // Instantiate func<int>
    }

According to C++17 specification, "static_cast<int*>(arg)" is NOT
type-dependent.
because "int*" is not dependent.
"17.6.2.2 Type-dependent expressions" says as follows:
    Expressions of the following forms are type-dependent only if the type 
    specified by the type-id, simple-type-specifier or new-type-id is
dependent, 
    even if any subexpression is type-dependent:
        ...(snip)...
        dynamic_cast <type-id> (expression)
        static_cast <type-id> (expression)
        const_cast <type-id> (expression)
        ...(snip)...

Therefore, "func_with_int_arg(static_cast<int*>(arg));" at (*1) should cause a
hard error 
instead of a substitution failure.

By the way, the above code does not compile fine with Clang 12.0.
The error message is:
    sample.cpp:9:9: error: no matching function for call to 'func_with_int_arg'
            func_with_int_arg(static_cast<int*>(arg));  // (*1)
            ^~~~~~~~~~~~~~~~~
    sample.cpp:3:10: note: candidate function not viable: no known conversion
from 'int *' to 'int' for 1st argument; dereference the argument with *
        void func_with_int_arg(int) {}
         ^
    1 error generated.

Regards,
Murase

Reply via email to