2010-11-15 69 views
1

我写了一个小级,以协助转换和从MSVC的笨重类型:这个简单的模板类有什么问题吗?

template <class FromType> 
struct convert 
{ 
    convert(FromType const &from) 
     : from_(from) {} 
    operator LARGE_INTEGER() { 
     LARGE_INTEGER li; 
     li.QuadPart = from_; 
     return li; 
    } 
private: 
    FromType const &from_; 
}; 

后来我这样做:

convert(0) 

,并从MSVC此错误消息:

1> e:\ src \ cpfs \ libcpfs \ device.cc(41):error C2955:'convert':使用类模板需要模板参数列表

1> E:\ SRC \ CPFS \ libcpfs \ device.cc(17):看 '转换'

我认为FromType可以从我传递的整数推断的声明?到底是怎么回事?

+0

U需要做这样的转换(0) – vinothkr 2010-11-15 05:30:17

回答

4

类模板从未隐式地实例化。鉴于你给的类定义,你不得不说:

convert<int>(0) 

...来调用类的构造函数。

在默认模板参数,你可以提高它(?):

template <class FromType = int> 
struct convert 
{ /* ... */ }; 

,然后调用它:

convert<>(0) 

...但我恐怕这是最好的你可以用类模板做。你可能反而想用一个函数模板实例化类对象为您提供:

template <typename FromType> 
convert<FromType> make_convert(FromType from) { 
    return convert<FromType>(from); 
} 

性病这是或多或少的方式使用:: make_pair()为例。

+0

谢谢。我去了一个转换工厂和转换器班。后来我把它们扔出去做手动,但是我:) – 2010-11-15 09:45:04

相关问题