Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板:是否覆盖部分(但不是全部)默认参数?_C++_Templates_Default Arguments - Fatal编程技术网

C++ C++;模板:是否覆盖部分(但不是全部)默认参数?

C++ C++;模板:是否覆盖部分(但不是全部)默认参数?,c++,templates,default-arguments,C++,Templates,Default Arguments,当我遇到一个带有多个默认参数的模板时,例如 template<typename ID = int, typename PAYLOAD = std::string> class Foo{}; 我可以写一些类似于: typedef Foo<PAYLOAD=int> Bar; typedef Foo-Bar; 我正在处理另一个团队的预定义模板,其中包含许多默认参数,并且能够使用第二种方法似乎可以提高代码的清晰度 查看有关StackOverflow的文档和其他问题,这似乎是

当我遇到一个带有多个默认参数的模板时,例如

template<typename ID = int, typename PAYLOAD = std::string>
class Foo{};
我可以写一些类似于:

typedef Foo<PAYLOAD=int> Bar;
typedef Foo-Bar;
我正在处理另一个团队的预定义模板,其中包含许多默认参数,并且能够使用第二种方法似乎可以提高代码的清晰度

查看有关StackOverflow的文档和其他问题,这似乎是不可能的,但我想知道是否有人可以解释为什么这不是一个功能

没有


不支持这样的模板类型。可以在左到右顺序指定类型,在Bjarne Stroustrup的设计和演化中,

,C++提到了对关键字参数的建议;但是,他们被拒绝了,因为

扩展组达成共识,即建议接近冗余,将导致与现有C++代码的兼容性问题,并会鼓励不应该鼓励的编程风格。


我想关键字模板参数也不包括在内,原因是相同的。如果你想简化你的生活,要么
typedef
消除复杂性,要么编写适配器类对模板参数重新排序。

如果模板只伪造类型,你可以用索引替换

创建绑定了前n个参数的模板。然后提取参数并替换所需的参数

这要求前n个参数在没有位置参数的情况下有效

草图:

 template<class...>struct types{using type=types;};
 template<class types, class T, size_t index>
 struct replace{
   using type=/*types with index replaced with `T`*/;
 };
 template<template<class...>class Z,class types>
 struct apply;// applies content of types to Z
 template<size_t n, class T>struct rep{};
 template<class Z, class...reps>
 struct replacing;
 template<template<class...>class Z, class...Ts,class...reps>
 struct replacing<Z<Ts...>,reps...>:
   apply<Z,replacing_t<types<Ts...>,reps...>>
 {};
 template<class...Ts,class T0,class n,class...reps>
 struct replacing<types<Ts...>,rep<n,T0>,reps...>:
   replacing<replace_t<types<Ts...>,T0,n>,reps...>
 {};
 template<class...Ts>
 struct replacing<types<Ts...>>:
   types<Ts...>
 {};

它使用
some_template
的默认参数,但将arg1替换为
int

Afaik。这仅在Python中可能(可能也在其他脚本语言中)。在C++中,我没有办法知道。这是解决方法,例如,也可以使用一些构造器模式来实现一个接近于: FuxBuilder::Apult::Type ,或者指定两个参数<代码> FooGuilder::Id::有效载荷:类型< /COD>(按任意顺序)。所有的参数类型都是吗?用索引覆盖可以吗?我知道这是委员会的意见,不一定是你的意见,但你认为你可以详细说明为什么这会被认为是“不应该鼓励的编程风格”?当然可以。在python中,看到10个或更多参数的函数并不少见,其中大多数参数具有默认值,这些参数都是由关键字参数使用的。有这么多的输入是一种糟糕的代码味道,因为这样的函数很难维护,而且不太连贯。建议对这些函数进行重构的目的是将相关参数提取到单独的结构中,以提供创建它们的简单方法。重要的是尽量保持较高的抽象级别
bool f(double,double,double,double,double,double)
不会告诉您任何事情,
bool f(Point&,Point&,double)
开始提示发生了什么:)对模板参数应用相同的推理。你很少需要超过3个——如果你需要,你的班级可能做得太多了。但是,如果你有一个简单的方法来模板化10个参数,这将鼓励人们这样做
 template<class...>struct types{using type=types;};
 template<class types, class T, size_t index>
 struct replace{
   using type=/*types with index replaced with `T`*/;
 };
 template<template<class...>class Z,class types>
 struct apply;// applies content of types to Z
 template<size_t n, class T>struct rep{};
 template<class Z, class...reps>
 struct replacing;
 template<template<class...>class Z, class...Ts,class...reps>
 struct replacing<Z<Ts...>,reps...>:
   apply<Z,replacing_t<types<Ts...>,reps...>>
 {};
 template<class...Ts,class T0,class n,class...reps>
 struct replacing<types<Ts...>,rep<n,T0>,reps...>:
   replacing<replace_t<types<Ts...>,T0,n>,reps...>
 {};
 template<class...Ts>
 struct replacing<types<Ts...>>:
   types<Ts...>
 {};
 replacing_t<some_template<>,rep<1,int>>