See the code below:
#include <iostream>
/// Definition of void perform(a_lib::a_class&). Where should I put this definition?
/// See the comments below for where I've tried placing it.
// void perform(a_lib::a_class&) {
// std::cout << "performing on a_lib::a_class" << std::endl;
// }
namespace a_lib {
class a_class { };
// WORKS HERE but it pollutes a_lib (namespace of third-party library).
}
namespace mine {
// DOESN'T WORK HERE: "use of undeclared identifier 'perform'".
}
// WORKS HERE but it pollutes the global namespace.
namespace b_lib {
// WORKS HERE but it pollutes b_lib (namespace of third-party library).
template <typename Type>
void b_func(Type& obj) {
perform(obj);
}
}
namespace mine {
// DOESN'T WORK HERE: "use of undeclared identifier 'perform'".
void run() {
a_lib::a_class a_obj;
b_lib::b_func(a_obj);
}
}
int main(int, char**) {
mine::run();
return 0;
}
a_lib
and b_lib
are namespaces belonging to two different third-party libraries. mine
is my own namespace.
I've been taught that it's a bad idea to pollute the global namespace and in this question they also say that adding types to std
is a bad idea. I'm thinking the latter is true for namespaces in general; you should not add types to a namespace that's not yours.
But how I would I solve the problem above without breaking these principles? Where should I put the definition of perform()
and how can I get b_func()
to call it?
Context: I'm trying to add an external cereal serialize() for a SFML type in my own namespace. This here is a reduced example.