Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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++ 模板是什么<;类型名T,T>;习语_C++_Templates_Idioms_Typename_C++14 - Fatal编程技术网

C++ 模板是什么<;类型名T,T>;习语

C++ 模板是什么<;类型名T,T>;习语,c++,templates,idioms,typename,c++14,C++,Templates,Idioms,Typename,C++14,我在看书,试图理解这是怎么回事。据说这个成语在网络搜索中经常出现,但我什么也找不到。问题是什么 template<typename T, T t> 模板 习语,它解决了什么,如何使用,隐式模板参数是什么,提案的目的是解决什么?正在解决的问题是从模板非类型参数推断类型 鉴于: template<typename T> void foo(T); template<typename T, T> void bar(); 这将允许编译bar(),并导致类型T被推断

我在看书,试图理解这是怎么回事。据说这个成语在网络搜索中经常出现,但我什么也找不到。问题是什么

template<typename T, T t>
模板

习语,它解决了什么,如何使用,隐式模板参数是什么,提案的目的是解决什么?

正在解决的问题是从模板非类型参数推断类型

鉴于:

template<typename T> void foo(T);
template<typename T, T> void bar();

这将允许编译
bar()
,并导致类型
T
被推断。

论文介绍有误导性:这个成语实际上是

 template <typename T, T t>
该提案是关于引入一点“语法糖”,使符号更容易编写


此外,上面给出的示例在其描述中是微不足道的(并且可能是错误的,因为模板参数需要是
constepr
),但本文描述了几种情况,其中当前的表示法可能变得非常复杂,降低了可读性和编程的总体易用性。

从模板
模板void foo(T)开始;。将该参数设为编译时值:
template void bar()(我想你的意思是,而不是
)。现在想想如何调用
foo(5)
使T成为
int
,但要使用
bar
,需要
bar()。方向正确吗?我是说T,不是T班。修好了。@chris,我知道你现在要去哪里了。对于我们提供的任何类型,bar都需要是泛型的,但也要将同一类型的值作为另一个模板参数。我们需要指定它的类型和值,以推断5是int。提案显示了在反射库中使用它,但直到今天我才看到它。除此之外,还可以如何使用?+1。啊!!我喜欢这个提议。我想它可能会帮助我解决C++11无法解决的问题。@Nawaz,说得好。实际上,我还没有考虑过C++11以这种方式不能允许什么。而且这种类型的值
t
必须是一个整型常量,或者是一个指针,因为在模板参数中不允许有其他值。
 template <typename T, T t>
// the current definition notation
template <typename T, T t> void f() { t.f(); };

//// the proposed definition notation
//// the parameter t depends on an implicit typename parameter T
// template <using typename T, T t> void f() { t.f(); };

struct foo {
    void f(){ 
        // some computation
    }
};

foo bar;

int main(){
    // the current instantiation notation
    f<foo,bar>(); 
    //// the proposed instantiation notation 
    //// we know that bar is of type foo, so we don't need to specify it
    // f<bar>();
}