Template procedure abandoned local class functions

Possible duplicate:
How to use local classes with templates?

g ++ 4.4 refuses to compile a call to a template function, taking a local-local class as a template parameter. For instance:

// Given this:
template <typename C>
int f(const C& c) {
  return c.g();
}

// This compiles fine:
struct C1 {
    int g() const { return 42; }
};

int h1() {
    return f(C1());
}

// But this doesn't:
int h2() {
    struct C2 {
        int g() const { return 42; }
    };
    return f(C2()); // error: no matching function for call to "f(h2()::C2)"
}

// Nor does this:
int h3() {
    struct C3 {
        int g() const { return 42; }
    };
    return f<C3>(C3()); // same error
}

What gives? How to do it? (In the real program from which it is truncated, “h” is a member function, and “C” must be a nested class, so that it is implicitly a friend of the class of which “h” is a member.)

+3
source share
3 answers

C ++ 0x will remove this unwanted restriction.

C ( h, h).

+2
+1

Template parameters must have an external link.

+1
source

Source: https://habr.com/ru/post/1757260/


All Articles