public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/53638] New: static_assert handling behavior ignores template specializations
@ 2012-06-11 20:40 sbraeger at knights dot ucf.edu
  2012-06-11 21:14 ` [Bug c++/53638] " redi at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: sbraeger at knights dot ucf.edu @ 2012-06-11 20:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53638

             Bug #: 53638
           Summary: static_assert handling behavior ignores template
                    specializations
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sbraeger@knights.ucf.edu


The current behavior of static_assert when inside a template definition is to
detect and attempt to evaluate the assert during the parser stage, before any
instantiations.  (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52809) 

"A static_assert declaration that does not depend on template parameters will
be
detected and reported while parsing the template, before any instantiation.
This is OK because such a template would have no valid instantiation, which
immediately renders the program ill-formed."

It is my opinion that this is not true.  It is possible that the template might
have valid specializations that occur later on in the compilation unit, making
it impossible to determine whether or not it is possible to construct an
instantiation of the template when parsing.

The relevant part of the standard says this " Refer to 14.6p8 in the spec

If no valid specialization can be generated for a template definition, and that
template is not instantiated, the template definition is ill-formed, no
diagnostic required.
"

This seems at first to imply that the current behavior is legal.  However, the
current behavior evaluates the static_assert and emits a message EVEN if valid
specializations CAN still be generated for a template definition.

Consider this code:

template<bool a>
struct s {
    static_assert(0, "uhoh");
};

template<>
struct s<false> {

};

s< false > q;  

This should compile, because s<false> is a valid specialization of s, meaning
that s<a> does not get instantiated.  However, on gcc 4.5.1 and gcc 4.7, we get

prog.cpp:3:5: error: static assertion failed: "uhoh"

Strangely, because of this behavior, we can 'fix' it by making the constant
expression dependent on a, even if we know it will always be false

template<bool a>
struct s {
    static_assert(a!=a, "uhoh" );
};

template<>
struct s<false> {

};

Compiles. 

The current behavior of GCC in this case MAY be conformant with spec, but it
seems to go against the intent of the spec even if it doesn't go against the
letter.  It also makes it difficult to use static_assert in certain
metaprogramming applications(like the example above, which could be used to
evaluate if a metafunction returned true).  Also, as bug 52809 demonstrates, it
is confusing to users.


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

end of thread, other threads:[~2023-02-18 21:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-11 20:40 [Bug c++/53638] New: static_assert handling behavior ignores template specializations sbraeger at knights dot ucf.edu
2012-06-11 21:14 ` [Bug c++/53638] " redi at gcc dot gnu.org
2012-10-05 13:37 ` paolo.carlini at oracle dot com
2023-02-18 21:19 ` cvs-commit at gcc dot gnu.org

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).