C ++模板非类型参数类型推导 [英] C++ template non-type parameter type deduction

查看:98
本文介绍了C ++模板非类型参数类型推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这项工作:

template < typename T, T VALUE >
void            f()
{
    /* ... */
}

int             main()
{
    f<10>();    // implicit deduction of [ T = int ] ??
    return (0);
}

目的是简化更复杂的模板。

The purpose is to simplify a much more complex template.

经过许多搜索,我没有找到任何办法在C ++ 0x,所以stackoverflow是我最后的手段。

After many searches, I don't find any way to do that on C++0x, so stackoverflow is my last resort.


  • 不指定所有类型的T可能...

  • 我在g ++ C ++ 0x,所以性感的东西是允许的。

推荐答案

C ++ 0x引入了 decltype()

C++0x introduces decltype(), which does exactly what you want.

int main()
{
  f<decltype(10), 10>(); // will become f<int, 10>();
  return 0;
}

这篇关于C ++模板非类型参数类型推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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