mardi 23 janvier 2018

Class template overloading across TUs

Consider the following C++11 application:

A.cpp:

template<typename T>
struct Shape {
    T x;
    T area() const { return x*x; }
};

int testA() {
    return Shape<int>{2}.area();
}

B.cpp:

template<typename T, typename U = T>
struct Shape {
    T x;
    U y;
    U area() const { return x*y; }
};
int testB() {
    return Shape<int,short>{3,4}.area();
}

Main.cpp:

int testA();
int testB();
int main() {
   return testA() + testB();
}

Although it compiles (as long as A and B are in separate TUs), it doesn't look right, and I'm having trouble figuring out why.

Hence my Question: Does this violate ODR, overloading, or any other rule, and if so, what sections of the Standard are violated and why?

Aucun commentaire:

Enregistrer un commentaire