English 中文(简体)
用高价值参考资料超载
原标题:Overloading with rvalue references

完美无缺。 但是,如果我想要而不是<>,那么我怎么做,那么,就什么时候,什么是模版?

template<typename T> void foo(T&& ref);
template<typename T> void foo(const T& ref); 

Won t work because the first overload will invoke perfect forwarding. I d really like behaviour very similar to if the first was a normal lvalue reference- where T will always be a value type.

最佳回答
#include <type_traits>

template<typename T>
typename std::enable_if
<
    !std::is_reference<T>::value,
    void
>::type
foo(T&& ref) {}

template<typename T> void foo(const T& ref) {}
问题回答

暂无回答




相关问题
Recursive lambda functions in C++11

I am new to C++11. I am writing the following recursive lambda function, but it doesn t compile. sum.cpp #include <iostream> #include <functional> auto term = [](int a)->int { ...

how to cache a lambda in c++0x?

I m trying to work with lambda s in C++ after having used them a great deal in C#. I currently have a boost tuple (this is the really simplified version). typedef shared_ptr<Foo> (*...

C++0x, Compiler hooks and hard coded languages features

I m a little curious about some of the new features of C++0x. In particular range-based for loops and initializer lists. Both features require a user-defined class in order to function correctly. I ...

How are you using C++11 today? [closed]

This is a question in two parts, the first is the most important and concerns now: Are you following the design and evolution of C++11? What blogs, newsgroups, committee papers, and other resources ...

C++ STL unordered_map problems and doubts

after some years in Java and C# now I m back to C++. Of course my programming style is influenced by those languages and I tend to feel the need of a special component that I used massively: the HASH ...