Making a constructor friend is possible in C++:
class A {
A() { B b; }
class B {
B(){};
friend A::A();
};
};
How can I achieve the same but with a template class:
template< typename T >
class EnforceOnlyOneIntConstructor
{
EnforceOnlyOneIntConstructor() {}
//friend T::T(int);
//friend T::OnlyOneIntConstructor(int);
friend void T::regularFun(int);
};
class OnlyOneIntConstructor
{
//EnforceOnlyOneIntConstructor< OnlyOneIntConstructor > m_trick;
public:
OnlyOneIntConstructor(int) //: m_trick{}
{}
void regularFun(int) {
EnforceOnlyOneIntConstructor< OnlyOneIntConstructor > m_trick;
}
};
int main() {
OnlyOneIntConstructor a(10);
}
I don’t know if it is even possible. Without commenting out the problematic sections the compiler gives an error:
error: C++ requires a type specifier for all declarations
Source: Windows Questions C++