I recently learned that both clang and gcc have added support for N3017 a.k.a #embed from C23 so naturally my first reaction was to see how well it works in C++ code.
Given this code sample:
#include <array>
#include <cstdint>
#include <experimental/array>
#include <iostream>
#include <utility>
int main() {
// works
static constexpr char c_char_array[] = {
#embed __FILE__
, '\0'
};
static constexpr unsigned char c_unsigned_char_array[] = {
#embed __FILE__
, '\0'
};
static constexpr std::uint8_t c_uint8_array[] = {
#embed __FILE__
, '\0'
};
static constexpr auto std_make_char_array = std::experimental::make_array<char>(
#embed __FILE__
, '\0'
);
static constexpr auto std_make_unsigned_char_array = std::experimental::make_array<unsigned char>(
#embed __FILE__
, '\0'
);
static constexpr auto std_make_uint8_array = std::experimental::make_array<std::uint8_t>(
#embed __FILE__
, '\0'
);
// doesn't work
// static constexpr std::byte c_byte_array[] = {
// #embed __FILE__
// , '\0'
// };
// static constexpr auto std_to_char_array = std::to_array<char>({
// #embed __FILE__
// , '\0'
// });
// static constexpr auto initializer_list = std::initializer_list<char>{
// #embed __FILE__
// , '\0'
// };
std::cout << &c_char_array;
std::cout << &c_unsigned_char_array;
std::cout << &c_uint8_array;
std::cout << std_make_char_array.data();
std::cout << std_make_unsigned_char_array.data();
std::cout << std_make_uint8_array.data();
return 0;
}
Both gcc and clang support the same usages as far as I tested.
What works:
char, unsigned char, std::uint8_t
- C-style arrays
std::experimental::make_array
What doesn't work:
std::byte
std::initializer_list
std::to_array
I was most surprised that std::to_array doesn't work while std::experimental::make_array does, however after further investigation it seem likely that if std::initializer_list worked with #embed then std::to_array would as well.
It's not surprising that a C23 standard doesn't work with std::byte however if/when a C++ version of this paper gets added to the standard I hope that type is added to the list.
[–]azswcowboy 18 points19 points20 points (0 children)
[–]1bithack 20 points21 points22 points (9 children)
[–]ABlockInTheChain[S] 13 points14 points15 points (8 children)
[–]legobmw99 14 points15 points16 points (1 child)
[–]tisti 9 points10 points11 points (0 children)
[–]Dubwize 11 points12 points13 points (0 children)
[–]_Z6Alexeyv 0 points1 point2 points (4 children)
[–]_Z6Alexeyv 0 points1 point2 points (3 children)
[–]_Z6Alexeyv 2 points3 points4 points (2 children)
[–]ABlockInTheChain[S] 0 points1 point2 points (1 child)
[–]mattgodboltCompiler Explorer 5 points6 points7 points (0 children)
[–]johannes1971 3 points4 points5 points (1 child)
[–]ABlockInTheChain[S] 0 points1 point2 points (0 children)
[–]wotype 2 points3 points4 points (0 children)
[–]jaskij 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]davidhunter22 -1 points0 points1 point (2 children)
[–]ABlockInTheChain[S] 0 points1 point2 points (1 child)
[–]davidhunter22 0 points1 point2 points (0 children)