English 中文(简体)
1. 参照标准
原标题:template pass by const reference

我审视了几个类似的问题,但我仍然混淆不清。 I m试图说明如何直截了当的(不是通过优化汇编等)和C++03-兼容的避免在将物体转至专用模板功能时复制物体。 这里是我的试验守则:

#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()

So I ve tried to specialize with a const C& parameter (2) to avoid this, but this simply doesn t work (apparently the reason is explained in this question).

当然,我可以“绕过点”,但这种ug。 因此,是否允许这样做?

EDIT:Oh,也许我很不清楚。 我已经有了一个模版的职能。

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

但我现在要专门履行这一职能,以接受一个组合;另一个目标:

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

但是,如果我把它定义为:

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

Basically what I want to do with this specialization is to adapt the SpecificObject to the template interface like

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

EDIT2:O,我可以强制使用<条码>const C&专业化,称为:

f<const C&>(c);

但它有办法使之像现在这样发挥作用:f(c)?

EDIT3:如果某个人最终会有类似问题,我最后在另一个问题上发现这一联系,这很有帮助:rel=“nofollow noreferer” http://www.gotw.ca/publications/mill17.htm

最佳回答

因此,如果你不总是想接受最恰当的参考(对基类[直线、长期、浮动等]来说是合理的),那么你就能够使用微薄的激素。

#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;
}
问题回答

你重新思考三个问题:模板、超负荷和争论的通过。

仅删除专业类别,并将论点作为<代码>。 T const&。

Cheers & hth.,

为什么你超负荷工作:

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

这将发挥作用:

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:cref(c)。

你们的问题是,实际参数c是底线,因此,主要模板比较合适,因为不需要添加这种编号。 如果你尝试具有价值和非最参照的功能,汇编者将告诉你,它无法解决这一差异。

#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;
}

这确实是你想要做的事,但你随后必须用最粗略的手法来对待一切已变成职务的价值观。 我不知道这是否是你所期望的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签