Basically, what I would like to be able to is to define operator++ for Test enum, which is a private member of a Inner class, which is private member of an Outer class. This snippet might be helpful in understanding what I want to acchieve:
class Outer{
class Inner{
enum Test{ONE, TWO, THREE};
};
};
const Outer::Inner::Test& operator++(Outer::Inner::Test& test){
int rawTest = test;
test = static_cast<Outer::Inner::Test>(++rawTest);
return test;
}
Unfortunately above will not compile with error, that Test is private member, so next I tried following:
#include <iostream>
using namespace std;
class Outer{
class Inner{
enum Test{ONE, TWO, THREE};
friend const Test& operator++(Test& test);
};
friend const Inner::Test& operator++(Inner::Test& test);
};
const Outer::Inner::Test& operator++(Outer::Inner::Test& test){
int rawTest = test;
test = static_cast<Outer::Inner::Test>(++rawTest);
return test;
}
this still does not work, because Test is defined in private Inner class, so I would have to friend both classes together, although there is no point in it (I do not want to access Test enum in Outer class. Just to be able to use operator++ in inner class)
Of course I can make function inline like this:
#include <iostream>
using namespace std;
class Outer{
class Inner{
enum Test{ONE, TWO, THREE};
friend const Test& operator++(Test& test){
int rawTest = test;
test = static_cast<Outer::Inner::Test>(++rawTest);
return test;
}
};
};
, but I might not want this, because of let's say some magical additional logic, which I want to have in .cpp file. How should I declare operator++ properly to be able to operator on Test enum?
EDIT: This is surely not a duplicate of given question, as I want to simply declare operator++ for given enum in nested class. The provided duplicate question is about accessing member of Inner class from an Outer class function.
EDIT: Bringing here Holt's comment: "Actually, clang accepts your second code (with the two friend declaration), which would be the standard way. Whether this is a clang extension or a g++ bug I don't know... Maybe you should reformulate your question to get the attention of some language-lawyer around there ;)" Maybe the more appropriate question would be, whether it is right, that gcc and msvc (which I tried) does not allow double fiendship code, or not, as in my opinion C++ standard should somehow allow clean coding in such cases like this one (in fact not so complicated case).