tags:

views:

57

answers:

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.)

+1  A: 

local class may not be template parameter.

http://stackoverflow.com/questions/2662843/c-can-local-class-reference-be-passed-to-a-function

aaa
Chapter and verse, please? Or link to further explication?
Zack
@Zac was looking for link, updated information
aaa
Thanks. How awkward.
Zack
@Zac might solve problem for you: http://www.boost.org/doc/libs/1_43_0/libs/spirit/phoenix/doc/html/index.html
aaa
+1  A: 

Template parameters must have extern linkage.

James Curran
+1  A: 

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
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.
Zack