次のコードを見てみましょう。
class const_int
{
public:
constexpr const_int(int data) : data_(data) {}
constexpr const_int(const const_int &) = default;
constexpr const_int(const_int &&) = default;
private:
int data_;
};
class test
{
public:
constexpr static const const_int USER = 42;
constexpr static const double NATIVE = 4.2;
};
// constexpr const const_int test::USER;
void pass_by_copie(double)
{
}
void pass_by_copie(const_int)
{
}
void pass_by_const_ref(const const_int&)
{
}
void pass_by_rvalue_ref(const_int&&)
{
}
int main(void)
{
pass_by_copie(test::NATIVE);
pass_by_copie(test::USER);
pass_by_const_ref(test::USER);
pass_by_rvalue_ref(const_int(test::USER));
return (0);
}
次の両方の行:
pass_by_copie(test::USER);
pass_by_const_ref(test::USER);
次のエラーが発生しますg++ 4.7:
`test::USER'への未定義の参照
のインスタンスがないことを認識していますtest::USER。(行は意図的にコメントされています)
2つの質問があります:
関数を呼び出すために
test::USERの明示的なインスタンスが必要ないのに、の明示的なインスタンスが必要なのはなぜですか?test::NATIVEpass_by_copieコンパイラがで呼び出すときに、同じコピーを自分で暗黙的に作成できない(または望まない)ときに
pass_by_rvalue_ref、一時コピーを明示的に作成して呼び出すことができるのはなぜですか?test::USERpass_by_copietest::USER
ありがとうございました