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_Pass By Reference_Parameter Passing_Pass By Value - Fatal编程技术网

C++ C+中的自动评估策略选择+;

C++ C+中的自动评估策略选择+;,c++,templates,pass-by-reference,parameter-passing,pass-by-value,C++,Templates,Pass By Reference,Parameter Passing,Pass By Value,考虑以下函数模板: template<typename T> void Foo(T) { // ... } 典型用法: template<typename T> class EnterpriseyObject { typedef typename Select<T>::type type; public: explicit EnterpriseyObject(type) { // ... } }; struct CustomT

考虑以下函数模板:

template<typename T> void Foo(T)
{
  // ...
}
典型用法:

template<typename T> class EnterpriseyObject
{
  typedef typename Select<T>::type type;

  public: explicit EnterpriseyObject(type)
  {
    // ...
  }
};

struct CustomType {};

void Usage()
{
  EnterpriseyObject<int>(0); // Pass-by-value.
  (EnterpriseyObject<CustomType>(CustomType())); // Pass-by-const-reference.
}
虽然这只是一个准便携式黑客

正如你所看到的,我的一般方法并不是对所有情况都满意


作为一名程序员爱好者,我既没有实际经验,也没有获得产品质量代码作为参考。我也意识到这似乎是一个过早优化的糟糕案例,但我真正感兴趣的是以下几点:

  • 您过去是否使用过这种类型的优化*
  • Boost
    (或任何其他公共)库是否已经提供了类似的功能
  • 如果#1或#2的答案是“是”——非类模板案例如何处理
  • 有没有什么明显的陷阱是我没有看到的
  • 最后,这是一件明智的事情吗

  • *未分析。;)

  • 对。总是我自己用
  • 是,使用:)

    用法将是

    template <typename T>
    void foo(boost::call_traits<T>::param_type param)
    {
        // Use param
    }
    
    模板
    void foo(boost::call_traits::param_type param)
    {
    //使用参数
    }
    
  • 据我所知,非类模板是按值传递的,除非速度更快。由于部分模板专门化,可以相对轻松地对其进行定制

  • 对不起,我没有读到你所做的,只是看起来和我几个月前所经历的一模一样。因此,我不能真正回答这个问题。我的建议是通读Boost.Utility

  • 当然


  • 杰出的在发布之前应该仔细查看。非常感谢我要提到
    template<typename T> void Foo(typename Select<T>::type)
    {
      // ...
    }
    
    void Usage()
    {
      Foo(0);      // Incomplete.
      Foo<int>(0); // Fine.
    }
    
    #define Foo(Arg) ::Foo<BOOST_TYPEOF((Arg))>((Arg))
    
    template <typename T>
    void foo(boost::call_traits<T>::param_type param)
    {
        // Use param
    }