Clang rejects std::array initialisation but gcc accepts

422 views Asked by At

I am learning about user defined literals and I wrote the following program that works with gcc and msvc but clang rejects it. Live demo

#include <array>

template<std::size_t N>
struct Literal
{
    std::array<char, N> arr;
    constexpr Literal(char const(&pp)[N]): arr(""){} 
};
 
template<Literal> 
constexpr auto operator""_S()
{
    return 4;
}
int main() {
    auto i = "test"_S;   
    auto j =  "ch"_S;       
}

I want to know which compiler is correct here. The error on clang says:

<source>:7:48: error: initializer-string for char array is too long, array size is 3 but initializer has size 5 (including the null terminating character)
    7 |     constexpr Literal(char const(&pp)[N]): arr(""){}
      |    
      <source>:17:15: note: in instantiation of member function 'Literal<3>::Literal' requested here
   17 |     auto j =  "ch"_S;
1

There are 1 answers

0
user12002570 On

This is a clang bug. Note that just changing the order of declaration of i and j makes clang accept the program.

Here is the relevant bug report:

Clang rejects valid program involving user defined literal