I'm sure we've all be told about how using "using namespace ____" can be bad in a global scope. I had a question about the use of locally
"using X = Y;" and whether it can cause problems later down the road.
When testing some code and changing features, I've been finding myself doing something like the following.
Each of these classes has different features, which I just represent for simplicity's sake as the print() member function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
struct Coordinate {
double x;
double y;
double z;
double w;
void print() {std::cout << "Coordinate" << std::endl;}
};
struct Vector2 {
double x; double y;
void print() {std::cout << "Vec2" << std::endl;}
};
struct Vector3 {
double x; double y; double z;
void print() {std::cout << "Vec3" << std::endl;}
};
int main()
{
using Coordinate = Vector2; // shadows the original Coordinate
//using Coordinate = Coordinate; // works too!
Coordinate c;
c.print();
}
|
When doing this I realized that the
using declarations in this way allows shadowing of types, seemingly without ambiguity.
If I want to change which implementation I want to use, I can easily just change the 1 line of "using" code to another.
I'm simply wondering if this could this cause problems for more complicated projects or do you agree that it's a good and efficient way for the coder to test and change to different implementations?
In what cases could this cause ambiguity, if any?