[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Functions as template arguments.
From: |
kwrzalik |
Subject: |
Functions as template arguments. |
Date: |
20 Jun 2006 08:23:21 -0700 |
User-agent: |
G2/0.2 |
Hello, I have this piece of code that reflects the problem in my real
program.
template<int (*foo)(int)> class templ { // 1
public: // 2
templ(int x) : _x(x) {} // 3
private: // 4
int _x; // 5
}; // 6
// 7
class example { // 8
public: // 9
void nop() const {} // 10
protected: // 11
static int intfunc(int x) {return x * 42; } // 12
static templ<intfunc> _internal; // 13
}; // 14
// 15
templ<example::intfunc> example::_internal(32); // 16
// 17
int main() { // 18
example ex; // 19
ex.nop(); // 20
} // 21
The code compiles smoothly with G++ 4.0.3, but gives me the following
errors with G++ 3.3.6:
</tmp> g++3 -Wall -c example.cc
example.cc:13: error: invalid use of undefined type `class example'
example.cc:8: error: forward declaration of `class example'
example.cc:13: error: template argument 1 is invalid
example.cc:13: error: ISO C++ forbids declaration of `_internal' with
no type
example.cc:12: error: `static int example::intfunc(int)' is protected
example.cc:16: error: within this context
example.cc:16: error: template argument 1 is invalid
example.cc:16: error: ISO C++ forbids declaration of `_internal' with
no type
Changing
static templ<intfunc> _internal; // 13
to
static templ<example::intfunc> _internal; // 13
gets rid of the "undefined type" non-sense, yet it still leaves the
following problems:
</tmp> g++3 -Wall -c example.cc
example.cc:12: error: `static int example::intfunc(int)' is protected
example.cc:16: error: within this context
example.cc:16: error: template argument 1 is invalid
example.cc:16: error: ISO C++ forbids declaration of `_internal' with
no type
example.cc:16: error: conflicting types for `int example::_internal'
example.cc:13: error: previous declaration as `templ<example::intfunc>
example::_internal'
What is wrong with this code? Why g++ 4 doesn't complain about it while
g++ 3 is unable to handle it?. What would be a universal method that
would allow to compile in both of them. In my real code _internal is a
cache that speeds up object creation (protected constructor + create,
you know the drill) and intfunc would be a hashing function. These are
implementation specific detail, therefore I wouldn't like to make them
public/global.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Functions as template arguments.,
kwrzalik <=