3

Possible Duplicate:
How to use local classes with templates?

g++ 4.4 is refusing to compile a call to a template function taking a function-local class as a template parameter. Like so:

// 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 do I make this work? (In the real program from which this is pruned, "h" is a member function, and "C" has to be a nested class so that it's implicitly a friend of the class of which "h" is a member.)

Community
  • 1
  • 1
zwol
  • 135,547
  • 38
  • 252
  • 361

3 Answers3

2

C++0x will remove this undesirable restriction.

For now, you can make C i a proper nested class (inside of h's class, not inside of h).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks for the workaround. It's unfortunate, because "C" is only used in that one place, but at least I don't have to make things public that shouldn't be. – zwol Jul 30 '10 at 17:05
1

local class may not be template parameter.

C++ can local class reference be passed to a function?

Community
  • 1
  • 1
Anycorn
  • 50,217
  • 42
  • 167
  • 261
1

Template parameters must have extern linkage.

James Curran
  • 101,701
  • 37
  • 181
  • 258