我不确定为什么下面的代码不能编译:
#include <array>
#include <algorithm>
#include <type_traits>
#include <concepts>
#include <cstdio>
#include <cwchar>
template <typename T>
concept Character = std::same_as< std::remove_cv_t<T>, char > ||
std::same_as< std::remove_cv_t<T>, signed char > ||
std::same_as< std::remove_cv_t<T>, unsigned char > ||
std::same_as< std::remove_cv_t<T>, wchar_t > ||
std::same_as< std::remove_cv_t<T>, char8_t > ||
std::same_as< std::remove_cv_t<T>, char16_t > ||
std::same_as< std::remove_cv_t<T>, char32_t >;
template <std::size_t N>
consteval auto create_character_array( std::integral auto&& fill_character ) noexcept
{
std::array<std::remove_cvref_t<decltype( fill_character )>, N> dashes { };
std::fill( std::begin( dashes ), std::end( dashes ), fill_character );
return dashes;
}
int main( )
{
const char ch { '^' };
auto dashes { create_character_array<128>( ch ) };
dashes.back( ) = '\0';
std::fputs( std::data( dashes ), stdout );
}
我不确定,但由于某种原因,通用参考(
auto&&
)和概念std::integral
似乎效果不佳。用我自己更精确的概念 std::integral
替换 Character
也无济于事。
出现以下错误:
<source>: In function 'int main()':
<source>:30:46: error: no matching function for call to 'create_character_array<128>(const char&)'
30 | auto dashes { create_character_array<128>( ch ) };
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
<source>:19:16: note: candidate: 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&)'
19 | consteval auto create_character_array( std::integral auto&& fill_character ) noexcept
| ^~~~~~~~~~~~~~~~~~~~~~
<source>:19:16: note: template argument deduction/substitution failed:
<source>:19:16: note: constraints not satisfied
In file included from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/compare:37,
from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/array:38,
from <source>:1:
/opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts: In substitution of 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&) [with long unsigned int N = 128; auto:16 = const char&]':
<source>:30:46: required from here
/opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:13: required for the satisfaction of 'integral<auto:16>' [with auto:16 = const char&]
/opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:24: note: the expression 'is_integral_v<_Tp> [with _Tp = const char&]' evaluated to 'false'
100 | concept integral = is_integral_v<_Tp>;
| ^~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:30:46: error: no matching function for call to 'create_character_array<128>(const char&)'
30 | auto dashes { create_character_array<128>( ch ) };
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
<source>:19:16: note: candidate: 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&)'
19 | consteval auto create_character_array( std::integral auto&& fill_character ) noexcept
| ^~~~~~~~~~~~~~~~~~~~~~
<source>:19:16: note: template argument deduction/substitution failed:
<source>:19:16: note: constraints not satisfied
In file included from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/compare:37,
from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/array:38,
from <source>:1:
/opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts: In substitution of 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&) [with long unsigned int N = 128; auto:16 = const char&]':
<source>:30:46: required from here
/opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:13: required for the satisfaction of 'integral<auto:16>' [with auto:16 = const char&]
/opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:24: note: the expression 'is_integral_v<_Tp> [with _Tp = const char&]' evaluated to 'false'
100 | concept integral = is_integral_v<_Tp>;
| ^~~~~~~~~~~~~~~~~~
当
'^'
作为参数传递时编译。然而ch
使它失败。这是在将 std::integral
添加到函数定义之后发生的。没有它,代码编译。
万一这不是一个好的设计,我应该如何定义上述功能以使其尽可能通用?我希望它接受任何值类别。