public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/103294] New: constexpr std::string does not work for clang
@ 2021-11-17  0:31 unlvsur at live dot com
  2021-11-17  0:45 ` [Bug libstdc++/103294] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: unlvsur at live dot com @ 2021-11-17  0:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103294
           Summary: constexpr std::string does not work for clang
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: unlvsur at live dot com
  Target Milestone: ---

Created attachment 51820
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51820&action=edit
error message from clang

#include<string>
#include<cassert>

constexpr bool foo()
{
        std::string str2{"abcwe"};
        return str2.size()==5;
}

static_assert(foo());

int main()
{
        assert(foo());
}

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

* [Bug libstdc++/103294] constexpr std::string does not work for clang
  2021-11-17  0:31 [Bug libstdc++/103294] New: constexpr std::string does not work for clang unlvsur at live dot com
@ 2021-11-17  0:45 ` pinskia at gcc dot gnu.org
  2021-11-17  0:46 ` unlvsur at live dot com
  2021-11-17  0:50 ` unlvsur at live dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-17  0:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC complains with -std=c++2b:
<source>: In function 'constexpr bool foo()':
<source>:7:33: error: call to non-'constexpr' function
'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>;
_CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]'
    7 |         std::string str2{"abcwe"};
      |                                 ^
In file included from
/opt/compiler-explorer/gcc-trunk-20211116/include/c++/12.0.0/string:53,
                 from <source>:2:
/opt/compiler-explorer/gcc-trunk-20211116/include/c++/12.0.0/bits/basic_string.h:535:7:
note: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>;
_CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]' declared here
  535 |       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
      |       ^~~~~~~~~~~~
<source>: At global scope:
<source>:11:18: error: non-constant condition for static assertion
   11 | static_assert(foo());
      |               ~~~^~
<source>:11:18: error: 'constexpr bool foo()' called in a constant expression
<source>:5:16: note: 'constexpr bool foo()' declared here
    5 | constexpr bool foo()
      |                ^~~

Even libc++ fails to compile the code:
<source>:7:14: error: variable of non-literal type 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char>>') cannot be defined in
a constexpr function
        std::string str2{"abcwe"};
                    ^
/opt/compiler-explorer/clang-trunk-20211116/bin/../include/c++/v1/string:679:5:
note: 'basic_string<char>' is not literal because it is not an aggregate and
has no constexpr constructors other than copy or move constructors
    basic_string
    ^
1 error generated.

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

* [Bug libstdc++/103294] constexpr std::string does not work for clang
  2021-11-17  0:31 [Bug libstdc++/103294] New: constexpr std::string does not work for clang unlvsur at live dot com
  2021-11-17  0:45 ` [Bug libstdc++/103294] " pinskia at gcc dot gnu.org
@ 2021-11-17  0:46 ` unlvsur at live dot com
  2021-11-17  0:50 ` unlvsur at live dot com
  2 siblings, 0 replies; 4+ messages in thread
From: unlvsur at live dot com @ 2021-11-17  0:46 UTC (permalink / raw)
  To: gcc-bugs

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

cqwrteur <unlvsur at live dot com> changed:

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

--- Comment #2 from cqwrteur <unlvsur at live dot com> ---
unintentional duplicated page. fixed

*** This bug has been marked as a duplicate of bug 103295 ***

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

* [Bug libstdc++/103294] constexpr std::string does not work for clang
  2021-11-17  0:31 [Bug libstdc++/103294] New: constexpr std::string does not work for clang unlvsur at live dot com
  2021-11-17  0:45 ` [Bug libstdc++/103294] " pinskia at gcc dot gnu.org
  2021-11-17  0:46 ` unlvsur at live dot com
@ 2021-11-17  0:50 ` unlvsur at live dot com
  2 siblings, 0 replies; 4+ messages in thread
From: unlvsur at live dot com @ 2021-11-17  0:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from cqwrteur <unlvsur at live dot com> ---
(In reply to Andrew Pinski from comment #1)
> GCC complains with -std=c++2b:
> <source>: In function 'constexpr bool foo()':
> <source>:7:33: error: call to non-'constexpr' function
> 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
> _CharT*, const _Alloc&) [with <template-parameter-2-1> =
> std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>;
> _Alloc = std::allocator<char>]'
>     7 |         std::string str2{"abcwe"};
>       |                                 ^
> In file included from
> /opt/compiler-explorer/gcc-trunk-20211116/include/c++/12.0.0/string:53,
>                  from <source>:2:
> /opt/compiler-explorer/gcc-trunk-20211116/include/c++/12.0.0/bits/
> basic_string.h:535:7: note: 'std::__cxx11::basic_string<_CharT, _Traits,
> _Alloc>::basic_string(const _CharT*, const _Alloc&) [with
> <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits =
> std::char_traits<char>; _Alloc = std::allocator<char>]' declared here
>   535 |       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
>       |       ^~~~~~~~~~~~
> <source>: At global scope:
> <source>:11:18: error: non-constant condition for static assertion
>    11 | static_assert(foo());
>       |               ~~~^~
> <source>:11:18: error: 'constexpr bool foo()' called in a constant expression
> <source>:5:16: note: 'constexpr bool foo()' declared here
>     5 | constexpr bool foo()
>       |                ^~~
> 
> Even libc++ fails to compile the code:
> <source>:7:14: error: variable of non-literal type 'std::string' (aka
> 'basic_string<char, char_traits<char>, allocator<char>>') cannot be defined
> in a constexpr function
>         std::string str2{"abcwe"};
>                     ^
> /opt/compiler-explorer/clang-trunk-20211116/bin/../include/c++/v1/string:679:
> 5: note: 'basic_string<char>' is not literal because it is not an aggregate
> and has no constexpr constructors other than copy or move constructors
>     basic_string
>     ^
> 1 error generated.

Your gcc is old and needs update. This is a new feature added today. But it
does not work clang.

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

end of thread, other threads:[~2021-11-17  0:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-17  0:31 [Bug libstdc++/103294] New: constexpr std::string does not work for clang unlvsur at live dot com
2021-11-17  0:45 ` [Bug libstdc++/103294] " pinskia at gcc dot gnu.org
2021-11-17  0:46 ` unlvsur at live dot com
2021-11-17  0:50 ` unlvsur at live 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).