Re: template constructor
Christof Warlich wrote:
is there any way to instantiate class A through
this constructor? I'd like to pass the value as
a template parameter instead of a constructor
argument for performance reasons. It will be part
of a very performance-critical library and the
parameter will always be known at compile time.
Thanks for any help,
Christof
#include <iostream>
class A {
public:
template<int x> A(void) {
std::cout << x << std::endl;
}
};
int main(void) {
// A a<27>; // does not work
}
For a templated constructor, the type (or value) deduction has to
happen from the argument. Since you don't have any arguments for
your constructor, it's impossible to provide the template argument
or make the compiler deduce it.
It sounds like you're trying to optimize your code before you know
it's going to be a performance hindrance. Knuth says, "Premature
optimization is the root of all evil". Take it to heart.
Give your constructor an integer argument. Unless you can prove
that in your final program having a constant in the code is better
than having a single integral value passed to the constructor as
its argument (and it better be _significantly better_) you are
wasting your valuable time.
Oh, BTW, drop the 'void' from the empty argument lists. An empty
list should be _empty_. This C-ism has no place in a C++ program
written from scratch.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask