The following code:
std::array<int, 4> arr1;
std::array<float, arr1.size()> arr2;
...compiles with both gcc and clang because std::array::size is considered constexpr.
But the following does not compile with gcc (version 5.3.0 20151204):
std::array<std::vector<int>, 4> arr1;
std::array<std::vector<double>, arr1.size()> arr2;
For me, there is no reason such code should fail to compile if the first one works but since I did not find a lot of post on this I don't know if it is a gcc bug or a clang extension?
The error from gcc (that I don't really understand... ):
main.cpp: In function 'int main()':
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
std::array<std::vector<double>, arr1.size()> arr2;
^
In file included from main.cpp:1:0:
/usr/local/include/c++/5.3.0/array:170:7: note: 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]' is not usable as a constexpr function because:
size() const noexcept { return _Nm; }
^
/usr/local/include/c++/5.3.0/array:170:7: error: enclosing class of constexpr non-static member function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]' is not a literal type
/usr/local/include/c++/5.3.0/array:89:12: note: 'std::array<std::vector<int>, 4ul>' is not literal because:
struct array
^
/usr/local/include/c++/5.3.0/array:89:12: note: 'std::array<std::vector<int>, 4ul>' has a non-trivial destructor
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
std::array<std::vector<double>, arr1.size()> arr2;
^
main.cpp:6:48: note: in template argument for type 'long unsigned int'
std::array<std::vector<double>, arr1.size()> arr2;
^
I think this is a related to CWG issue 1684. Previously, the constexpr requirements included:
The class of which a constexpr function is a member shall be a literal type
And std::array<std::vector<int>, 4> is not a literal type, and hence its size() member function would not be constexpr. However, the new wording allows for a constexpr non-static member functions for non-literal types, assuming those functions meet all the other requirements of constexpr (which array<T,N>::size() clearly does).
Per the new wording, this is a gcc bug. Previously filed as 66297.
Related
I am getting some error in a templated function of some 3rd party library that I am trying to build.
MSVC points to the function and told me that I am doing something wrong for a specific call. How can I know at which call exactly the error happens?
If it matters, this is the function:
template <typename T>
std::string ToString(T number) {
std::ostringstream ss;
ss << std::setprecision(NUM_TO_STRING_PRECISION);
ss << number;
return ss.str();
}
The error is:
Error C2088 '<<': illegal for class
How can I know at which call exactly the error happens?
Not really with visual-studio, but let's look at a simple example:
#include <vector>
struct Foo {
Foo() = delete;
};
int main() {
std::vector<Foo> vfoo(15);
(void)vfoo;
}
GCC g++ outputs these error messages:
In file included from /usr/local/include/c++/7.1.0/vector:63:0,
from main.cpp:1:
/usr/local/include/c++/7.1.0/bits/stl_uninitialized.h: In instantiation of 'static _ForwardIterator std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]':
/usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:583:20: required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]'
/usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:645:44: required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Foo*; _Size = long unsigned int; _Tp = Foo]'
/usr/local/include/c++/7.1.0/bits/stl_vector.h:1347:36: required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]'
/usr/local/include/c++/7.1.0/bits/stl_vector.h:285:30: required from 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Foo>]'
main.cpp:9:29: required from here
/usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:548:37: error: use of deleted function 'Foo::Foo()'
return std::fill_n(__first, __n, _ValueType());
^~~~~~~~~~~~
main.cpp:4:5: note: declared here
Foo() = delete;
^~~
IIRC it's very similar in visual studio. Just open the raw output tab, and go to the very last note, which probably contains the real source of the error.
MSVC results with rextester. Again the very last notes for source_file.cpp point to the real source of the error.
The following code compiles well:
int main()
{
variant<any> var;
var = 5;
cout << any_cast<int>(get<any>(var)) << endl;
return 0;
}
But when I'm trying to put variant<any> as a class member
struct MyClass{
variant<any> var;
};
int main()
{
MyClass s;
return 0;
}
It doesn't compile. Am I doing something wrong or it is some bug?
I'm using gcc 7.1.0
In file included from /home/zak/Projects/Anytest/main.cpp:3:0:
/usr/local/gcc-7.1/include/c++/7.1.0/variant: In instantiation of ‘struct std::__detail::__variant::__accepted_index<const std::variant<std::any>&, std::variant<std::any>, void>’:
/usr/local/gcc-7.1/include/c++/7.1.0/variant:911:26: required from ‘constexpr const size_t std::variant<std::any>::__accepted_index<const std::variant<std::any>&>’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:940:6: required by substitution of ‘template<class _Tp, class> constexpr std::variant<std::any>::variant(_Tp&&) [with _Tp = const std::variant<std::any>&; <template-parameter-1-2> = <missing>]’
/home/zak/Projects/Anytest/main.cpp:14:13: required from here
/usr/local/gcc-7.1/include/c++/7.1.0/variant:559:49: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
{ static void _S_fun(); };
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate expects 0 arguments, 1 provided
/usr/local/gcc-7.1/include/c++/7.1.0/variant:559:49: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
{ static void _S_fun(); };
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate expects 0 arguments, 1 provided
/usr/local/gcc-7.1/include/c++/7.1.0/variant:559:49: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
{ static void _S_fun(); };
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate expects 0 arguments, 1 provided
/usr/local/gcc-7.1/include/c++/7.1.0/variant: In instantiation of ‘constexpr const size_t std::__detail::__variant::__accepted_index<const std::variant<std::any>&, std::variant<std::any>, void>::value’:
/usr/local/gcc-7.1/include/c++/7.1.0/variant:911:26: required from ‘constexpr const size_t std::variant<std::any>::__accepted_index<const std::variant<std::any>&>’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:940:6: required by substitution of ‘template<class _Tp, class> constexpr std::variant<std::any>::variant(_Tp&&) [with _Tp = const std::variant<std::any>&; <template-parameter-1-2> = <missing>]’
/home/zak/Projects/Anytest/main.cpp:14:13: required from here
/usr/local/gcc-7.1/include/c++/7.1.0/variant:564:12: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
- decltype(__overload_set<_Types...>::
~~~~~~~~~~~~~~~~~~~~~~~~~~~
_S_fun(std::declval<_Tp>()))::value;
~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
{ static void _S_fun(); };
^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate expects 0 arguments, 1 provided
The problem is with MyClass's implicitly defined copy constructor, when it tries to copy construct the member of type std::variant<std::any>.
To perform overload resolution, the compiler first needs to try to instantiate all of std::variant's constructor templates, with the function argument being a const std::variant<std::any>&1. The constructor of our interest is this one:
template <class T> constexpr variant(T&& t) noexcept(/*...*/);
It only participates in overload resolution if, among others, the expression FUN(std::forward<T>(t)) is well formed, where FUN is a set of overloaded functions produced according to [variant.ctor]/12.2
In this case, there is only one alternative type (std::any), so there is only one imaginary function FUN, whose signature is FUN(std::any). Now, the compiler needs to decide whether FUN can be called with a const std::variant<std::any>&1. In this process, the compiler needs to know whether std::any can be constructed with const std::variant<std::any>&1.
This will trigger the instantiation of std::any's constructor template template<class T> any(T&& value);, which only participates in overload resolution if std::is_copy_constructible_v<VT> is true (VT being std::decay_t<T>, and T being const std::variant<std::any>&).
Now in order to see whether VT (i.e. std::variant<std::any>) is copy constructible, the compiler needs to try to instantiate all of std::variant's constructor templates... and this is where we started, and we are stuck in a loop.
This can explain why we see template<class _Tp, class> constexpr std::variant<std::any>::variant(_Tp&&) and __overload_set<std::any>::_S_fun (which corresponds to the function FUN mentioned above) in the error message, and why we see the same error appearing several times.
It remains a question how GCC breaks from the above loop, and why tweaking the program can stop GCC from reporting the error. Maybe these are indication of some bug.
1. Strictly speaking, it should be "an lvalue of type const std::variant<std::any>" rather than "a const std::variant<std::any>&".
2. The standard also requires that this constructor shall only participate in overload resolution if is_same_v<decay_t<T>, variant> is false. GCC (libstdc++) chooses to check that later. I don't know whether this is conforming.
The following code:
std::array<int, 4> arr1;
std::array<float, arr1.size()> arr2;
...compiles with both gcc and clang because std::array::size is considered constexpr.
But the following does not compile with gcc (version 5.3.0 20151204):
std::array<std::vector<int>, 4> arr1;
std::array<std::vector<double>, arr1.size()> arr2;
For me, there is no reason such code should fail to compile if the first one works but since I did not find a lot of post on this I don't know if it is a gcc bug or a clang extension?
The error from gcc (that I don't really understand... ):
main.cpp: In function 'int main()':
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
std::array<std::vector<double>, arr1.size()> arr2;
^
In file included from main.cpp:1:0:
/usr/local/include/c++/5.3.0/array:170:7: note: 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]' is not usable as a constexpr function because:
size() const noexcept { return _Nm; }
^
/usr/local/include/c++/5.3.0/array:170:7: error: enclosing class of constexpr non-static member function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]' is not a literal type
/usr/local/include/c++/5.3.0/array:89:12: note: 'std::array<std::vector<int>, 4ul>' is not literal because:
struct array
^
/usr/local/include/c++/5.3.0/array:89:12: note: 'std::array<std::vector<int>, 4ul>' has a non-trivial destructor
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
std::array<std::vector<double>, arr1.size()> arr2;
^
main.cpp:6:48: note: in template argument for type 'long unsigned int'
std::array<std::vector<double>, arr1.size()> arr2;
^
I think this is a related to CWG issue 1684. Previously, the constexpr requirements included:
The class of which a constexpr function is a member shall be a literal type
And std::array<std::vector<int>, 4> is not a literal type, and hence its size() member function would not be constexpr. However, the new wording allows for a constexpr non-static member functions for non-literal types, assuming those functions meet all the other requirements of constexpr (which array<T,N>::size() clearly does).
Per the new wording, this is a gcc bug. Previously filed as 66297.
If I compile the code below:
#include <list>
using iter_t = std::list<const unsigned>::const_iterator;
int main(int, char**) {
return 0;
}
I get the following compile error:
In file included from /usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/x86_64-apple-darwin13.4.0/bits/c++allocator.h:33:0,
from /usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/allocator.h:46,
from /usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/list:61,
from main.cpp:1:
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<const unsigned int>':
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/allocator.h:92:11: required from 'class std::allocator<const unsigned int>'
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/stl_list.h:315:9: required from 'class std::_List_base<const unsigned int, std::allocator<const unsigned int> >'
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/stl_list.h:447:11: required from 'class std::list<const unsigned int>'
main.cpp:3:41: required from here
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h:93:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const unsigned int; __gnu_cxx::new_allocator<_Tp>::const_pointer = const unsigned int*; __gnu_cxx::new_allocator<_Tp>::const_reference = const unsigned int&]' cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
^
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h:89:7: error: with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = const unsigned int; __gnu_cxx::new_allocator<_Tp>::pointer = const unsigned int*; __gnu_cxx::new_allocator<_Tp>::reference = const unsigned int&]'
address(reference __x) const _GLIBCXX_NOEXCEPT
^
I get roughly the same error message if I change the container type from std::list to std::vector, std::deque or std::forward_list. If I change the template argument from const unsigned to unsigned it compiles fine.
What's going on here? I can't see why it wouldn't compile.
Visual C++ 2015 gives the clarifying diagnostic
” The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.
As I understand it, this is because an allocator's allocate method returns a pointer to an array of uninitialized T items, and if T is const then these items can never be initialized.
I can't find any explicit statement that allocator<const T> is ill-formed, so it is presumably a consequence of the semantics.
As Dieter Lücking notes in a comment, there is a direct clash for const type T between the two member functions that provide address of a reference and const_reference, because these are equal when the type is const.
Example code test.cpp
#include <array>
#include <string>
int main ()
{
// OK
const std::array<int, 2> array_int = {42, 1337};
std::array<float, array_int.size()> array_float_ok;
// Error
const std::array<std::string, 2> array_string = {"foo", "bar"};
std::array<float, array_string.size()> array_float_error;
return 0;
}
Compiling with g++ 4.8.4 (Ubuntu 14.04)
g++ -Wall -std=c++0x test.cpp -o test
Gives the following error message
test.cpp: In function ‘int main()’:
test.cpp:14:39: error: call to non-constexpr function ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’
std::array<float, array_string.size()> array_float_error;
^
In file included from test.cpp:1:0:
/usr/include/c++/4.8/array:162:7: note: ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’ is not usable as a constexpr function because:
size() const noexcept { return _Nm; }
^
/usr/include/c++/4.8/array:162:7: error: enclosing class of constexpr non-static member function ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’ is not a literal type
/usr/include/c++/4.8/array:81:12: note: ‘std::array<std::basic_string<char>, 2ul>’ is not literal because:
struct array
^
/usr/include/c++/4.8/array:81:12: note: ‘std::array<std::basic_string<char>, 2ul>’ has a non-trivial destructor
test.cpp:14:39: error: call to non-constexpr function ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’
std::array<float, array_string.size()> array_float_error;
^
test.cpp:14:40: note: in template argument for type ‘long unsigned int’
std::array<float, array_string.size()> array_float_error;
^
test.cpp:14:59: error: invalid type in declaration before ‘;’ token
std::array<float, array_string.size()> array_float_error;
^
test.cpp:9:39: warning: unused variable ‘array_float_ok’ [-Wunused-variable]
std::array<float, array_int.size()> array_float_ok;
^
test.cpp:14:42: warning: unused variable ‘array_float_error’ [-Wunused-variable]
std::array<float, array_string.size()> array_float_error;
^
Can someone explain this error? Why does the first example work while the second one does not compile?
The type std::string is not a literal type meaning that it cannot be manipulated as part of a constexpr function at compile time. At compile time, the compiler attempts to evaluate the array_string's size() function. The functions first type parameter as you can see in the first error is set to std::basic_string < char > (aka std::string); therefore, since std::string is not a literal type, the function cannot be evaluated as a constexpr function at compile time and you have an error.
I would refer you to the following to learn more about constexpr.
http://en.cppreference.com/w/cpp/language/constexpr
I would refer you to the following to learn about literal types.
http://en.cppreference.com/w/cpp/concept/LiteralType
Finally, try the following simple code and you will see that int and float are literal types and std::string is not. You can try it out with other types to see what are or aren't literal types.
#include <iostream>
int main(int argc, char** argv)
{
std::cout << std::is_literal_type<int>::value << std::endl;
std::cout << std::is_literal_type<float>::value << std::endl;
std::cout << std::is_literal_type<std::string>::value << std::endl;
return 0;
}
Hope that helps.
John