Back to questions
JuniorTheoryOccasional
What happens when you build and run this program?
#include <iostream>
using Meters = double;
void print(double) { std::cout << "double"; }
void print(Meters) { std::cout << "Meters"; }
int main() {
Meters m = 2.5;
print(m);
}Compile error: print(double) is defined twice
- Expecting
Meters: an alias is another name fordouble, not a new type, soprint(Meters)is a second definition ofprint(double). - Expecting a link error: the compiler already sees both definitions in one file and stops there; the linker reports only duplicates from different files.
- How could you make
Metersa distinct type thatprintcan be overloaded on? - How does
using Meters = double;differ from#define Meters double?