例如,下面的代码片段编译在VC++ 2010:是否有依赖于较早参数的默认模板参数?
template<int Rows, int Columns = Rows>
struct Matrix { };
Matrix<4> m;
注意,对于Columns
默认参数取决于为Rows
参数值。
但是,这是标准行为在C++ 11(或更早),我可以依靠到处?
例如,下面的代码片段编译在VC++ 2010:是否有依赖于较早参数的默认模板参数?
template<int Rows, int Columns = Rows>
struct Matrix { };
Matrix<4> m;
注意,对于Columns
默认参数取决于为Rows
参数值。
但是,这是标准行为在C++ 11(或更早),我可以依靠到处?
是的。事实上,这就是大量STL代码的工作原理。
的std::vector
具有类似的定义:
template < class T, class Alloc = allocator<T> > class vector
,这样你就不需要每次每次都指定allocator
。如果这样无效,我们将不能写:
std::vector<int> data;
而且你会写std::map
为:
std::map < keyType, // map::key_type
ValType, // map::mapped_type
less<keyType>, // map::key_compare
allocator<pair<const KeyType,ValType> > // map::allocator_type
> mapping;
比远不太理想:
std::map< keyType , ValType > mapping;
据cplusplus,是:
也可以设置默认值或类型的类模板参数。例如,如果前面的模板类的定义已经:
template <class T=char, int N=10> class mysequence {..};
而且在一个比较通俗的音符,G ++ -Wall会编译它。
当然,这并没有发生在我身上。谢谢! – Cameron 2013-02-16 06:22:08