当一个函数采用shared_ptr(来自boost或C++ 11 STL)时,你传递它:
通过const引用: void foo(const shared_ptr<T>& p)
或按价值:void foo(shared_ptr<T> p)?
我更喜欢第一种方法,因为我怀疑它会更快.但这真的值得吗还是还有其他问题吗?
您能否说出您选择的原因或案例,为什么您认为无关紧要.
我试图理解完美转发和构造函数的相互作用.我的例子如下:
#include <utility>
#include <iostream>
template<typename A, typename B>
using disable_if_same_or_derived =
std::enable_if_t<
!std::is_base_of<
A,
std::remove_reference_t<B>
>::value
>;
template<class T>
class wrapper {
public:
// perfect forwarding ctor in order not to copy or move if unnecessary
template<
class T0,
class = disable_if_same_or_derived<wrapper,T0> // do not use this instead of the copy ctor
> explicit
wrapper(T0&& x)
: x(std::forward<T0>(x))
{}
private:
T x;
};
class trace {
public:
trace() {}
trace(const trace&) { std::cout << "copy ctor\n"; }
trace& operator=(const …Run Code Online (Sandbox Code Playgroud)