throw_away_account throw_away_account - 1 month ago 12
C++ Question

Classes with same name in different namespace

I am writing a library. Is it a bad idea to have two classes with same name but each of the classes are in different namespace?

Using name spaces:

//forward declaration
namespace sparse {
class matrix;
}
namespace dense {
class matrix;
}

namespace dense {
typedef Index uint64_t;
class matrix {
public:
sparse::matrix tosparse();
};
}

namespace sparse {
typedef Index uint32_t;
class matrix {
dense::matrix todense();
};
}


Alternatively, I can use long names:

class sparse_matrix
...

Answer

In general, that's fine. That's exactly what namespaces are for. In this example it's not clear what the design goal is, so it's not clear whether this is an appropriate use of namespaces.