C++ 常量表达式在作为部分而不是单个函数完成时起作用
该代码对a、b、a、W有效,但对J无效。 这是完全相同的代码刚刚被分解。 有什么好处 我试图让“make”作为一个恒定的表达。 对于编译器来说,在这个简单的示例中失败的代码并没有那么复杂 很奇怪C++ 常量表达式在作为部分而不是单个函数完成时起作用,c++,templates,c++17,constexpr,C++,Templates,C++17,Constexpr,该代码对a、b、a、W有效,但对J无效。 这是完全相同的代码刚刚被分解。 有什么好处 我试图让“make”作为一个恒定的表达。 对于编译器来说,在这个简单的示例中失败的代码并没有那么复杂 很奇怪 template<typename... Args> constexpr auto all2(Args... args) noexcept { return static_cast<int>((... + args)); } struct xx { int y = 2;
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;
}
template constexpr
自动所有2(参数…参数)无例外
{return static_cast((…+args));}
结构xx
{
int y=2;
constexpr运算符int()const noexcept{return y;}
};
模板
结构pepe2
{
constexpr pepe2(Args…Args){}
};
模板<类型名。。。T_ARGS>constexpr
自动生成(常量参数和…参数)无例外
{
返回pepe2{};
}
int main()
{
//这是意料之中的事
constexpr静态xx a{};
constexpr静态xx b{};
constexpr静态自动A{all2(A,b)};
constexpr静态pepe2w{a,b};
//但事实并非如此!!!
constexpr静态自动J=制造(a,b);
返回0;
}
来自叮当声的实际错误
:21:24:错误:非类型模板参数不是常量表达式
返回pepe2{};
^
:33:35:注意:在函数模板的实例化中,此处请求专门化“make”
constexpr静态自动J=制造(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;
}
#包括
模板constexpr
自动所有2(参数…参数)无例外
{return static_cast((…+args));}
结构xx
{
int y=2;
constexpr运算符int()const noexcept{return y;}
};
模板
结构pepe2
{
constexpr pepe2(Args…{}
};
模板<自动&。。。T_ARGS>constexpr
自动生成()无例外
{
返回pepe2{T_ARGS…};
}
int main()
{
//这是意料之中的事
constexpr静态xx a{};
constexpr静态xx b{};
constexpr静态自动A{all2(A,b)};
constexpr静态pepe2w{a,b};
//这也适用于现在
constexpr static auto J=make();
返回0;
}
#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;
}