let’s take the following code:
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);
}
Both of the following lines :
pass_by_copie(test::USER);
pass_by_const_ref(test::USER);
produce the following error under g++ 4.7 :
undefined reference to `test::USER’
I am aware of the fact that there is no instance of test::USER. (the line is commented on purpose)
I have two questions :
-
Why is an explicit instance of
test::USERneeded while no explicit instance oftest::NATIVEis needed to call the functionpass_by_copie? -
Why can i call
pass_by_rvalue_refby explicity creating a temporary copie fromtest::USERwhile the compiler is unable (or doesn’t want) to create the same copie implicitly himself when callingpass_by_copiewithtest::USER?
Thank you
Clang 4.1 gives me this error:
I’m assuming what’s going on here is
test::NATIVEcan be simply replaced with the value4.2where it’s used, and doesn’t actually require a symbol. Buttest::USER, being an instance of a class rather than a scalar, does require a symbol so that all references totest::USERrefer to the same object. Given this, you actually do need the explicit instance.