b******n 发帖数: 592 | 1 0 vector a;
1 C<0>(a); // container
I tried to implement C as
template
class C{
const T & mT;
public:
C(const T& t) : mT(t) {};
}
Compiler always complains about T is missing in 1. Is there any way to get
around this? |
e****d 发帖数: 895 | 2 Use a template function to generate C<>.
【在 b******n 的大作中提到】 : 0 vector a; : 1 C<0>(a); // container : I tried to implement C as : template : class C{ : const T & mT; : public: : C(const T& t) : mT(t) {}; : } : Compiler always complains about T is missing in 1. Is there any way to get
|
s*w 发帖数: 729 | 3 你的 vector 没类型啊? 貌似你想做的是 template specification. N 也没用上啊,
去了得了
【在 b******n 的大作中提到】 : 0 vector a; : 1 C<0>(a); // container : I tried to implement C as : template : class C{ : const T & mT; : public: : C(const T& t) : mT(t) {}; : } : Compiler always complains about T is missing in 1. Is there any way to get
|
t****t 发帖数: 6806 | 4 you can not let compiler deduce template parameter when you name a typename.
as someone said, you need a generator:
template
C gen_c(const T& t) { return C(t); }
then you can let compiler deduce:
auto ca=gen_c<0>(a);
this technique is widely used in STL, for example binder1st (class) and
bind1st (generator).
【在 b******n 的大作中提到】 : 0 vector a; : 1 C<0>(a); // container : I tried to implement C as : template : class C{ : const T & mT; : public: : C(const T& t) : mT(t) {}; : } : Compiler always complains about T is missing in 1. Is there any way to get
|
b******n 发帖数: 592 | 5 I did use a generator function. didn't like it though. thought there is an
other way to get around it. thanks. every time I thought I know C++, a test
shows there are much to learn.
typename.
【在 t****t 的大作中提到】 : you can not let compiler deduce template parameter when you name a typename. : as someone said, you need a generator: : template : C gen_c(const T& t) { return C(t); } : then you can let compiler deduce: : auto ca=gen_c<0>(a); : this technique is widely used in STL, for example binder1st (class) and : bind1st (generator).
|