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
(或任何其他公共)库是否已经提供了类似的功能*未分析。;)
template <typename T>
void foo(boost::call_traits<T>::param_type param)
{
// Use param
}
模板
void foo(boost::call_traits::param_type param)
{
//使用参数
}
杰出的在发布之前应该仔细查看。非常感谢我要提到
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
}