public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent.
@ 2020-08-22 16:04 masamitsu.murase at gmail dot com
  2020-08-24 23:24 ` [Bug c++/96746] " mpolacek at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: masamitsu.murase at gmail dot com @ 2020-08-22 16:04 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/96746] Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent.
  2020-08-22 16:04 [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent masamitsu.murase at gmail dot com
@ 2020-08-24 23:24 ` mpolacek at gcc dot gnu.org
  2020-11-04 15:21 ` masamitsu.murase at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-08-24 23:24 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I don't think it's a bug, probably more of a [temp.res]/8 issue:

The program is ill-formed, no diagnostic required, if:
-- no valid specialization can be generated for a template or a substatement of
a constexpr if statement within a template and the template is not
instantiated, or
[...]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/96746] Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent.
  2020-08-22 16:04 [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent masamitsu.murase at gmail dot com
  2020-08-24 23:24 ` [Bug c++/96746] " mpolacek at gcc dot gnu.org
@ 2020-11-04 15:21 ` masamitsu.murase at gmail dot com
  2020-11-04 15:37 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: masamitsu.murase at gmail dot com @ 2020-11-04 15:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Masamitsu MURASE <masamitsu.murase at gmail dot com> ---
Thank you very much for your reply, Marek Polacek.

I'm sorry, but I cannot understand your comment clearly.
As you said, I also think that my example should be treated as ill-formed
program.
It **compiles fine** with GCC 11.0 even though it is ill-formed.

It seems that clang and MSVC cause hard error appropriately.

Regards,
Murase

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/96746] Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent.
  2020-08-22 16:04 [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent masamitsu.murase at gmail dot com
  2020-08-24 23:24 ` [Bug c++/96746] " mpolacek at gcc dot gnu.org
  2020-11-04 15:21 ` masamitsu.murase at gmail dot com
@ 2020-11-04 15:37 ` redi at gcc dot gnu.org
  2020-11-04 15:41 ` jakub at gcc dot gnu.org
  2021-01-01 14:22 ` masamitsu.murase at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-11-04 15:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The standard says "no diagnostic required". It's not a bug if a compiler
accepts the code.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/96746] Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent.
  2020-08-22 16:04 [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent masamitsu.murase at gmail dot com
                   ` (2 preceding siblings ...)
  2020-11-04 15:37 ` redi at gcc dot gnu.org
@ 2020-11-04 15:41 ` jakub at gcc dot gnu.org
  2021-01-01 14:22 ` masamitsu.murase at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-11-04 15:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
What Marek explained is that your testcase is buggy, but it may or may not be
diagnosed, unless the template is instantiated.  If you instantiate such
template, it has to be rejected by all implementations, but when only
performing semantic analysis of uninstantiated templates, different
implementations will report different sets of bugs, some are easier to diagnose
in some implementations, others in different ones.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/96746] Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent.
  2020-08-22 16:04 [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent masamitsu.murase at gmail dot com
                   ` (3 preceding siblings ...)
  2020-11-04 15:41 ` jakub at gcc dot gnu.org
@ 2021-01-01 14:22 ` masamitsu.murase at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: masamitsu.murase at gmail dot com @ 2021-01-01 14:22 UTC (permalink / raw)
  To: gcc-bugs

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

Masamitsu MURASE <masamitsu.murase at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Masamitsu MURASE <masamitsu.murase at gmail dot com> ---
Jonathan Wakely, Jakub Jelinek,
Thank you very much for your explanation.
I totally understand that both gcc and clang meet the C ++ specification.

Let me mark this issue as "Resolved" and "Invalid".

Regards,
Murase

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-01-01 14:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-22 16:04 [Bug c++/96746] New: Type Casting in template function should not be type-dependent if the type of the conversion result is not type-dependent masamitsu.murase at gmail dot com
2020-08-24 23:24 ` [Bug c++/96746] " mpolacek at gcc dot gnu.org
2020-11-04 15:21 ` masamitsu.murase at gmail dot com
2020-11-04 15:37 ` redi at gcc dot gnu.org
2020-11-04 15:41 ` jakub at gcc dot gnu.org
2021-01-01 14:22 ` masamitsu.murase at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).