The following code compiles in GCC (4.9.3) and VC ++ (19.00.23506), but gives this error in Clang (3.7.0).
error: constexpr function return type 'Foo' is not a literal type
note: 'Foo' is not literal because it is not a collection and does not have constexpr constructors other than copy or move constructors
the code:
#include <iostream>
#include <vector>
struct Foo
{
std::vector<int> m_vec;
Foo(const int *foo, std::size_t size=0):m_vec(foo, foo+size)
{;}
};
template <std::size_t N>
constexpr Foo make_fooArray(const int (&a)[N]) noexcept
{
return {a,N};
}
int main()
{
Foo f{ make_fooArray({1,2,3}) };
for (auto i : f.m_vec)
std::cout<< i <<" ";
std::cout<<std::endl;
}
The code works on the registry:
GCC and VC
Clang
Can you clarify if this is a compiler error or am I missing something? What does the C ++ 11 standard say?
Here is another case where it compiles in GCC and VC, but not in Clang.
#include <iostream>
template <typename T, std::size_t N>
constexpr std::size_t sizeOf_fooArray(const T (&)[N]) noexcept
{
return N;
}
int main()
{
std::cout<< sizeOf_fooArray({16,20,53,87,54,7}) <<std::endl;
}
, int [] initializer_list, .
#include <iostream>
template <typename T, std::size_t N>
constexpr std::size_t sizeOf_fooArray(const T (&)[N]) noexcept
{
return N;
}
using intArray = int[];
int main()
{
std::cout<< sizeOf_fooArray(intArray{16,20,53,87,54,7}) <<std::endl;
}