constexpr とマークされた関数は、不変の純粋な関数であると想定されています。「std::max() and std::min() not constexpr」投稿から、const 参照入力を出力として再チャネル化することはできません。これには、パラメーターに永続性が必要になるためです。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() ); }
逆に、-enabled 型のサブオブジェクトへの 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 ]; }
};
それとも、サブオブジェクトの戻り値でさえも値渡しである必要がありますか?
(これが基本的なものである場合は申し訳ありませんが、実際に決定的ではなく、この時点で私が見つけたものはすべてダンスです。)