This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/53573] template type dependent name resolution broken


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53573

--- Comment #5 from Keean Schupke <keean@fry-it.com> 2012-06-04 17:13:18 UTC ---
The following program will apparently compile correctly, but will silently
produce incorrect results at runtime:



#include <iostream>

char g(char x) {
    return x - 1;
}

template <typename T> T f(T t) {
    return g(t);
}

int g(int x) {
    return x + 1;
}

main() {
    int x(1);
    int y(f(x));
    std::cout << y << "\n";
    int z(f<int>(x));
    std::cout << z << "\n";
}



This should print "2 2" but prints "0 0". Interestingly it also produces
incorrect results with gcc-4.6.3 and gcc-4.5.3. However the code below (which
does not compile on gcc-4.7.0 does produce the correct results on gcc-4.6.3 and
gcc-4.5.3:



#include <iostream>

template <typename T> T f(T t) {
    return g(t);
}

char g(char x) {
    return x - 1;
}

int g(int x) {
    return x + 1;
}

main() {
    int x(1);
    int y(f(x));
    std::cout << y << "\n";
    int z(f<int>(x));
    std::cout << z << "\n";
}



So it looks like earlier versions of GCC relied on 'g' being undefined at the
point of definition to defer binding until instantiation instead of looking for
dependence on a template parameter. gcc-4.7.0 does not seem to do either.

One more example:



#include <iostream>

class A {};

A g(A x) {
    return x;
}

template <typename T> T f(T t) {
    return g(t);
}

int g(int x) {
    return x + 1;
}

main() {
    int x(1);
    int y(f(x));
    std::cout << y << "\n";
    int z(f<int>(x));
    std::cout << z << "\n";
}



generates the following compile error:

test.cpp: In function âT f(T) [with T = int]â:
test.cpp:35:11:   instantiated from here
test.cpp:26:12: error: could not convert âtâ from âintâ to âAâ


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]