【发布时间】:2012-03-04 12:57:31
【问题描述】:
标记为 constexpr 的函数应该是不可变的纯函数。从"std::max() and std::min() not constexpr" 帖子中,您不能将 const-reference 输入重新引导为输出,因为这需要参数具有永久性。但是你可以通过const-reference 取一个参数,只要你不重新引导它吗?
// Is this still constexpr?
// (Assuming that std::complex is constexpr-safe, as it's supposed to be.)
constexpr
int MySum( std::complex<double> const &a, std::complex<double> const &b )
{ return static_cast<int>( a.real() + b.real() ); }
相反,您能否将 const 引用返回到启用 constexpr 的类型的子对象?
template <typename T>
class MyComplex
{
T c_[ 2 ];
public:
constexpr MyComplex( T r = T(), T i = T() )
: c_{ r, i }
{}
// Is this actually constexpr?
constexpr T const & operator[]( unsigned l ) //const
{ return c_[ l ]; }
// Can't be constexpr
T & operator[]( unsigned l ) { return c_[ l ]; }
};
或者甚至子对象的返回也必须按值返回?
(对不起,如果这是基本的,但我发现的所有内容都围绕这一点跳舞,实际上并没有确定性。)
【问题讨论】:
标签: c++ c++11 pass-by-reference constexpr