C ++:对于除最后的其它模板参数的默认值? [英] C++: Default values for template arguments other than the last ones?

查看:104
本文介绍了C ++:对于除最后的其它模板参数的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的模板容器类,看起来像这样:

I have my templated container class that looks like this:

template<
   class KeyType, 
   class ValueType, 
   class KeyCompareFunctor   = AnObnoxiouslyLongSequenceOfCharacters<KeyType>, 
   class ValueCompareFunctor = AnObnoxiouslyLongSequenceOfCharacters<ValueType> 
>
   class MyClass
   {
      [...]
   }

这意味着当我实例化这个类的一个对象,我能做到这一点几种不同的方式:

Which means that when I instantiate an object of this class, I can do it several different ways:

MyClass<MyKeyType, MyValueType> myObject;
MyClass<MyKeyType, MyValueType, MyCustomKeyCompareFunctor> myObject;
MyClass<MyKeyType, MyValueType, MyCustomKeyCompareFunctor, MyCustomValueCompareFunctor> myObject;

这些都是很好的。问题是当我想要实例化使用ValueCompareFunctor参数的非缺省版本MyClass的,但我还是想用KeyCompareFunctor参数的默认值。然后,我必须写这篇文章:

Those are all good. The problem comes when I want to instantiate a MyClass that uses a non-default version of the ValueCompareFunctor argument, but I still want to use the default value of the KeyCompareFunctor argument. Then I have to write this:

MyClass<MyKeyType, MyValueType, AnObnoxiouslyLongSequenceOfCharacters<MyKeyType>, MyCustomValueCompareFunctor> myObject;

这将是方便多了,如果我能以某种方式省略了第三个参数,只是这样写:

It would be much more convenient if I could somehow omit the third argument and just write this:

MyClass<KeyType, ValueType, MyCustomValueCompareFunctor> myObject;

由于MyCustomValueCompareFunctor仅适用于类型MyValueType的对象,而不是类型MyKeyType的对象,好像编译器可能至少在理论上锻炼了我的意思在这里。

Since the MyCustomValueCompareFunctor works only on objects of type MyValueType and not on objects of type MyKeyType, it seems like the compiler could at least theoretically work out what I meant here.

有没有办法在C这样做++?

Is there a way to do this in C++?

推荐答案

在一般情况下,无论是在模板和函数或方法,C ++,您可以使用默认值(从而省略)唯一的结尾的参数 - - 没有出路

In general, both in templates and functions or methods, C++ lets you use default for (and thereby omit) only trailing parameters -- no way out.

我推荐一个模板或宏缩短 AnObnoxiouslyLongSequenceOfCharacters&LT; MyKeyType&GT; 美孚&LT; MyKeyType&GT; - 不完美,但有总比没有好。

I recommend a template or macro to shorten AnObnoxiouslyLongSequenceOfCharacters<MyKeyType> to Foo<MyKeyType> -- not perfect, but better than nothing.

这篇关于C ++:对于除最后的其它模板参数的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆