【问题标题】:template pass by const reference模板通过 const 引用传递
【发布时间】:2011-02-02 15:04:53
【问题描述】:

我已经查看了一些类似的问题,但我仍然感到困惑。我试图弄清楚如何显式(而不是通过编译器优化等)和C++03兼容避免在将对象传递给specialized模板时复制它功能。这是我的测试代码:

#include <iostream>

using namespace std;

struct C
{
  C() { cout << "C()" << endl;  }
  C(const C&) { cout << "C(C)" << endl; }
  ~C() { cout << "~C()" << endl;  }
};

template<class T> void f(T) { cout << "f<T>" << endl; }

// This shows two possible ways, I don't need two overloads
// If I do it like (2) the function is not called, only if I do it like (1)

template<> void f(C c) { cout << "f<C>" << endl; } // (1)

template<> void f(const C& c) { cout << "f<C&>" << endl; } // (2)

int main()
{
    C c;
    f(c);
    return 0;
}

(1) 接受C 类型的对象,并进行复制。这是输出:

C()
C(C)
f<C>
~C()
~C()

所以我尝试专门使用 const C&amp; 参数 (2) 来避免这种情况,但这根本不起作用(显然原因在 this question 中有解释)。

好吧,我可以“通过指针”,但这有点难看。那么是否有一些技巧可以很好地做到这一点?

编辑:哦,可能我不清楚。我已经有一个模板函数

template<class T> void f(T) {...}

但是现在我想专门化这个函数来接受一个 const& 到另一个对象:

template<> void f(const SpecificObject&) {...}

但只有当我将其定义为时才会调用它

template<> void f(SpecificObject) {...}

基本上我想用这个专业化做的是使SpecificObject适应模板界面,比如

template<> void f(SpecificObject obj){ f(obj.Adapted()); } // call the templated version

EDIT2:好的,我可以强制以这种方式调用 const C&amp; 专业化:

f<const C&>(c);

但是有没有办法让它像 f(c) 这样工作?

EDIT3:如果有人最终会有类似的问题,我终于在另一个问题中找到了这个链接,它很有帮助:http://www.gotw.ca/publications/mill17.htm

【问题讨论】:

  • 而且我找不到定义这两个专业化的原因,为什么必须区分 const 引用和复制。而且我认为没有办法区分它们。
  • @mattia.penati:这不是我的本意,实际上......
  • 好的,现在很清楚了。首先,如果要防止函数调用中的隐式复制,可以将explicit 放在复制构造函数的前面。现在您可以将函数f 定义为template &lt;class T&gt; f(T const &amp;)。否则使用重载。没有别的办法。
  • @mattia.penati:我不想改变原来的模板化函数,只是为了让传递引用与专门的函数一起工作......
  • 使用重载。否则别无他法。

标签: c++ templates template-specialization


【解决方案1】:

您将三个问题混为一谈:模板、重载和参数传递。

只需删除特化并将参数作为T const&amp; 传递。

干杯,

【讨论】:

  • @7views:只需删除f 的特化。那么你就剩下一个函数模板了。将其形式参数类型更改为T const&amp;。干杯&hth
  • 我明白了,我的问题还不够清楚 :( 我试图澄清。
【解决方案2】:

你为什么不超载:

void f(const C& c) { cout << "f(const C&)" << endl; }

【讨论】:

  • 这将是按值版本的模棱两可的重载。
  • 我这样做了,但它不起作用。请参阅 (2) 和我链接到的问题。
  • 好吧,如果我省略 template&lt;&gt; 它似乎可以工作,但我不想超载,我想通过引用传递一个 模板
【解决方案3】:

这可行:

int main()
{
    C c;
    f<const C&>(c);
    return 0;
}

你的选择:

template<typename T> void f(const boost::reference_wrapper<T const>& c) 
    { cout << "f<boost_const_ref&>" << endl; } 

int main()
{
    C c;
    f(boost::cref(c));
    return 0;
}

实际上,您会使用 boost::reference_wrapper 将引用传递到您想要使用它的位置。你可以使用 get() 来做到这一点,尽管 boost::reference_wrapper 有一个隐式转换回引用,所以你可能不需要模板的部分专业化,只需将 boost::cref(c) 传递给常规模板。

【讨论】:

    【解决方案4】:

    因此,如果您不总是希望接受 const 引用(这对于基类型 [int、long、float 等] 是合理的),您可以使用一点 boost 魔法。

    #include <iostream>
    #include <boost/call_traits.hpp>
    
    using namespace std;
    
    struct C
    {
      C() { cout << "C()" << endl;  }
      C(const C&) { cout << "C(C)" << endl; }
      //C& operator=(C const&) { cout << "C=C" << endl; return *this; }
      ~C() { cout << "~C()" << endl;  }
    };
    
    template<class T> void foo(typename boost::call_traits<T>::param_type inst) { cout << "f<T>" << endl; }
    // specialization for calling class C
    template<> void foo<C>(boost::call_traits<C>::param_type inst) { cout << "f<C>" << endl; }
    
    int main()
    {
      int i = 0;
      foo<int>(i);
      C c;
      foo<C>(c);
      return 0;
    }
    

    【讨论】:

    • 我的模板函数不需要接受 const 引用,所以它是 f&lt;T&gt;。但我希望我的专业接受const SpecificObject&amp;,即f&lt;const SpecificObject&amp;&gt;,但我不知道如何以这种方式进行专业化。
    • @7vies,这就是call_traits 的美妙之处,它自动推断出对于SpecificObject,它必须通过一个常量引用,所以上面的特化将被调用。对于其他类型,将调用生成的foo(通过值或常量引用)
    • @Nim:好吧,但是为什么你有两次param_type,第二个情况不应该是const_reference吗?编辑:啊,我读过param_type 的意思,所以我现在明白了......
    • @7vies,你可以改变它,我只是保持原样,它在编译时减少到相同的东西。
    • @7vies, call_traits 通常在您希望允许编译器决定什么是传递参数的最佳方式时很有用(通常这在您的类模板或函数模板中非常有用)不知道是什么类型)。它肯定会在参数传递中可能存在任何歧义的地方提供帮助,让编译器做出正确的决定。
    【解决方案5】:

    您的问题是实际参数 c 不是 const,因此主模板是更好的匹配,因为它不需要将“const”添加到类型中。如果您尝试通过值和非常量引用传递的函数,编译器会告诉您它无法解决该差异。

    【讨论】:

      【解决方案6】:
      #include <iostream>
      
      using namespace std;
      
      struct C
      {
        C() { cout << "C()" << endl;  }
        C(const C&) { cout << "C(C)" << endl; }
        ~C() { cout << "~C()" << endl;  }
      };
      
      template<class T> void f(const T&) { cout << "f<T>" << endl; }
      
      int main()
      {
          C c;
          f(c);
          return 0;
      }
      

      这确实可以满足您的需求,但是您必须对传递给函数的所有值使用 const ref。我不知道这是否是你要找的。​​p>

      【讨论】:

      • 啊,我明白了。我的第一个想法是,这可能会给您带来问题,因为始终首选通过确切类型的传递。但我可能错了。
      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多