Skip to content

[libc++] std::vector<char[16]> error: cannot initialize return object of type char *[16] with an rvalue of type char * #98734

@AngryLoki

Description

@AngryLoki

According to https://stackoverflow.com/a/63259370/337979, the following code should work since c++11:

#include <vector>

void test(){
	std::vector<char[16]> alpha(10);
	static_assert(sizeof(alpha[0]) == 16, "Size of each element in alpha is not 16 bytes");
}

It works with libstdc++ (both in gcc/clang), but fails with libc++: https://godbolt.org/z/qnn1Kehz4

$ clang++ -std=c++23 --stdlib=libstdc++ main.cpp -c -o main.o
# ok!

$ clang++ -std=c++23 --stdlib=libc++ main.cpp -c -o main.o
In file included from main.cpp:1:
In file included from /usr/include/c++/v1/vector:308:
In file included from /usr/include/c++/v1/__algorithm/copy.h:12:
In file included from /usr/include/c++/v1/__algorithm/copy_move_common.h:18:
In file included from /usr/include/c++/v1/__string/constexpr_c_functions.h:14:
/usr/include/c++/v1/__memory/construct_at.h:41:10: error: cannot initialize return object of type 'char *[16]' with an rvalue of type
      'char *'
   41 |   return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__memory/construct_at.h:49:15: note: in instantiation of function template specialization
      'std::construct_at<char[16], char *>' requested here
   49 |   return std::construct_at(__location, std::forward<_Args>(__args)...);
      |               ^
/usr/include/c++/v1/__memory/allocator_traits.h:305:10: note: in instantiation of function template specialization
      'std::__construct_at<char[16], char *>' requested here
  305 |     std::__construct_at(__p, std::forward<_Args>(__args)...);
      |          ^
/usr/include/c++/v1/vector:1057:21: note: in instantiation of function template specialization
      'std::allocator_traits<std::allocator<char[16]>>::construct<char[16], void, void>' requested here
 1057 |     __alloc_traits::construct(this->__alloc(), std::__to_address(__pos));
      |                     ^
/usr/include/c++/v1/vector:1122:5: note: in instantiation of member function 'std::vector<char[16]>::__construct_at_end' requested here
 1122 |     __construct_at_end(__n);
      |     ^
main.cpp:4:24: note: in instantiation of member function 'std::vector<char[16]>::vector' requested here
    4 |         std::vector<char[16]> alpha(10);
      |                               ^
1 error generated.

It looks similar to #40629, but probably different as char[16] is trivially destructible.

Checked with clang version 18.1.8+libcxx and libcxx-18.1.8. Discovered in https://github.com/ROCm/hipBLASLt/blob/f192cdec31dbdca581c8df03c3980b9cc5b678c7/library/src/amd_detail/rocblaslt/src/rocblaslt_mat.cpp#L605

Metadata

Metadata

Assignees

Labels

libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.wontfixIssue is real, but we can't or won't fix it. Not invalid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions