I am having trouble calling a member function of a singleton class. I have the following
// my_class.hpp
class my_class {
friend struct singleton;
int m_x;
my_class(int x)
: m_x(x) {}
// other private constructors here
public:
const auto& get() const {
return m_x;
}
};
struct singleton {
static auto& instance() {
static my_class x(42);
return x;
}
};
When trying to call singleton::instance().get()
outside of main(), the compiler says "expected function body after function declarator". Here is the relevant main file:
singleton::instance().get();
int main() {}
I have found that moving the call inside main everything works fine. I have also found that storing the result outside of main also works fine, like so
auto v = singleton::instance().get();
int main() {}
What I'd like to understand is why calling it outside of main() fails and if there is a workaround that doesn't require storing the result. Thank you.