#include <memory>
class CItem
{
private:
int m_inner;
public:
static const int CAP = 1;
CItem(int temp) : m_inner(temp) {}
};
typedef std::shared_ptr<CItem> TPItem;
int main()
{
int tttt = CItem::CAP;
CItem *temp = new CItem(CItem::CAP);
TPItem temp2(temp);
TPItem temp3 = std::make_shared<CItem>(tttt);
TPItem temp4 = std::make_shared<CItem>(CItem::CAP); //On MinGW there error: "undefined reference to `CItem::CAP'"
return 0;
}
- on Visual Studio 2012 it work normal.
- on minGW32 4.9.1 it say "undefined reference" for static const field of class when try create shared pointer with make_shared.
- other methods:
- copy this field to int and create shared with this int - work.
- create class object with new - work.
Where my fault?
This is because
CItem::CAP
is odr-used bystd::make_shared
(emphasis mine):Since std::make_shared takes its arguments by reference, this counts as an odr-use.
which means you are required to provide an out of class definition as well:
or avoid the odr-use such as in this case:
For reference the draft C++11 standard section
3.2
[basic.def.odr] says:CItem::CAP
is a constant expression and in all the cases except this one:the lvalue-to-rvalue conversion is applied immediately.