什么是模板< typename T,T t>成语? [英] What is the template<typename T, T t> idiom?

查看:121
本文介绍了什么是模板< typename T,T t>成语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读,并试图了解 N3601 是关于。它说这个成语在网络搜索中出现了很多,但我找不到任何东西。什么是

 模板< typename T,T t& 

idiom,它是如何解决的,如何使用,什么是隐式模板参数,

解决方案

要解决的问题是从模板非类型参数中推导出类型。 p>

给定:

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

可以推导出 T code> foo (例如 foo(10)会导致 T 被推断为 int ),但是不可能为 bar推导 T bar< 10>()将不会编译,你必须将它写为 bar< int,10& ())。



N3601 建议通过引入以下语法修复此问题:

  template< using类型名T,T> void bar(); 

这将允许 bar< 10>()来编译并导致 T 的类型被推导出来。


I was reading this and was trying to understand what N3601 was about. It said this idiom comes up a lot in a web search, but I couldn't find anything. What is the

template<typename T, T t>

idiom, what does it solve, how is it used, what is are Implicit template parameters, and what does the proposal aim to fix?

解决方案

The problem that is being solved is deducing types from template non-type parameters.

Given:

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

it is possible to deduce T for foo (for example, foo(10) will result in T being deduced to be int), but it is not possible to deduce T for bar (bar<10>() will simply not compile, you have to write it as bar<int,10>()).

N3601 proposes fixing this by introducing the syntax:

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

which will allow bar<10>() to compile and cause the type T to be deduced.

这篇关于什么是模板&lt; typename T,T t&gt;成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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