恒定表达式在完成时起作用,而不是单个功能
Constant expression works when is done as parts but not as a single function
代码适用于A,B,A,W,但对于J.这是完全相同的代码刚刚分解。什么给?
我试图使"使"起作用作为恒定的表达。对于编译器而言,该代码在此简单示例上失败并不那么复杂。
非常奇怪...
template<typename... Args> constexpr
auto all2(Args... args) noexcept
{ return static_cast<int>((... + args)); }
struct xx
{
int y = 2;
constexpr operator int () const noexcept { return y; }
};
template<int C, typename... Args>
struct pepe2
{
constexpr pepe2( Args... args ){}
};
template< typename... T_ARGS > constexpr
auto make( const T_ARGS&... args ) noexcept
{
return pepe2< all2(args...), T_ARGS... >{};
}
int main()
{
// This works as expected
constexpr static xx a {};
constexpr static xx b {};
constexpr static auto A { all2( a, b ) };
constexpr static pepe2<A,xx,xx> W { a, b };
// But this does not!!!
constexpr static auto J = make( a, b );
return 0;
}
clang的实际错误
<source>:21:24: error: non-type template argument is not a constant expression
return pepe2< all2(args...), T_ARGS... >{};
^
<source>:33:35: note: in instantiation of function template specialization 'make<xx, xx>' requested here
constexpr static auto J = make( a, b );
函数参数不是 constexpr
。实际上,make
函数可以采用非costexpr参数。因此,我们不能将它们用于模板实例化。
函数参数不是常数表达式。您可以将a
和b
作为模板参数传递。
#include <type_traits>
template<typename... Args> constexpr
auto all2(Args... args) noexcept
{ return static_cast<int>((... + args)); }
struct xx
{
int y = 2;
constexpr operator int () const noexcept { return y; }
};
template<int C, typename... Args>
struct pepe2
{
constexpr pepe2( Args... ){}
};
template< auto&... T_ARGS > constexpr
auto make() noexcept
{
return pepe2< all2(T_ARGS...), std::decay_t<decltype(T_ARGS)>... >{T_ARGS...};
}
int main()
{
// This works as expected
constexpr static xx a {};
constexpr static xx b {};
constexpr static auto A { all2( a, b ) };
constexpr static pepe2<A,xx,xx> W { a, b };
// This also works now
constexpr static auto J = make<a, b>();
return 0;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 单个模板功能,用于显示不同容器的内容
- 单个功能中的多个参数包
- 仅适用于单个功能的部分模板专业化
- 恒定表达式在完成时起作用,而不是单个功能
- 支持单个位偏移的类似"memcpy"的功能?