Declaring a namespace as a friend of a class

No, it’s not possible befriend a namespace. If nothing else, it would constitute a “security breach,” as namespaces can be extended anywhere. So anyone could add an arbitrary function to the namespace and access the class’s non-public data. The closest you can get is the solution you propose, making those functions static members of a … Read more

c++ error C2662 cannot convert ‘this’ pointer from ‘const Type’ to ‘Type &’

CombatEventType getType(); needs to be CombatEventType getType() const; Your compiler is complaining because the function is being given a const object that you’re trying to call a non-const function on. When a function gets a const object, all calls to it have to be const throughout the function (otherwise the compiler can’t be sure that … Read more

C++ compile time counters, revisited

After further investigation, it turns out there exists a minor modification that can be performed to the next() function, which makes the code work properly on clang++ versions above 7.0.0, but makes it stop working for all other clang++ versions. Have a look at the following code, taken from my previous solution. template <int N> … Read more

How to make std::make_unique a friend of my class

make_unique perfect forwards the arguments you pass to it; in your example you’re passing an lvalue (x) to the function, so it’ll deduce the argument type as int&. Your friend function declaration needs to be friend std::unique_ptr<A> std::make_unique<A>(T&); Similarly, if you were to move(x) within CreateA, the friend declaration would need to be friend std::unique_ptr<A> … Read more

Operator overloading : member function vs. non-member function?

If you define your operator overloaded function as member function, then the compiler translates expressions like s1 + s2 into s1.operator+(s2). That means, the operator overloaded member function gets invoked on the first operand. That is how member functions work! But what if the first operand is not a class? There’s a major problem if … Read more