3

我发现了在标准 C++11 中使用可选引用的技巧。您是否认为这种技术可靠并且行为根据 C++ 标准得到了很好的定义?

// Optional reference using C++11 
// V. Reverdy - 2013
#include <iostream>
#include <type_traits>

template <class T = int, class = typename std::enable_if<std::is_same<typename std::decay<T>::type, int>::value>::type>
void f(T&& n = T())
{
    std::cout<<"--------"<<std::endl;
    std::cout<<"is const = "<<std::is_const<T>::value<<std::endl;
    std::cout<<"is reference = "<<std::is_reference<T>::value<<std::endl;
    std::cout<<"is lvalue reference = "<<std::is_lvalue_reference<T>::value<<std::endl;
    std::cout<<"is rvalue reference = "<<std::is_rvalue_reference<T>::value<<std::endl;
    std::cout<<"--------"<<std::endl;
    n *= 2;
} 

int main(int argc, char* argv[])
{
    int n = 42;
    std::cout<<"n = "<<n<<std::endl;
    f();
    std::cout<<"n = "<<n<<std::endl;
    f(n);
    std::cout<<"n = "<<n<<std::endl;
    return 0;
}

结果是:

n = 42
--------
is const = 0
is reference = 0
is lvalue reference = 0
is rvalue reference = 0
--------
n = 42
--------
is const = 0
is reference = 1
is lvalue reference = 1
is rvalue reference = 0
--------
n = 84

它似乎适用于 liveworkspace 上所有可用的编译器:LWS

4

2 回答 2

2

笏。

首先,你已经“实现”的东西可以通过重载来简单得多。其次,它与可选引用完全不同。可选引用是一个值,它可能包含也可能不包含运行时的引用。行为是明确定义的,但它既不可取也不是可选的参考。在某些情况下,将临时引用绑定为默认参数很好,但它距离可选引用有 10 亿英里。

于 2013-03-13T13:40:46.197 回答
1

它是明确定义的,是的,因为“通用引用”(请注意这是一个非标准术语)可以解析为右值引用并因此绑定到临时对象,但您没有创建“可选引用”。

引用必须始终初始化并且必须始终绑定到对象,它们不能“绑定或不绑定”(这是我对“可选引用”的意思的直观理解)。

在这里,您只是将该引用绑定到一个临时对象。您所做的相当于以下内容:

template<typename T> void f(T&& t) { ... }
void f() { f(int()); }

如果您的意思是说您现在能够创建一个接受引用并且可以在不提供任何参数的情况下调用的函数 - 这是 IMO 更正确的说法 - 那么这是真的,但我不会考虑它一个突破性的发现。

即使在 C++03 中,您也可以做类似的事情,尽管仅供参考const

template<typename T>
void f(T const& = T()) { ... }

由于 C++11 引入了const可以绑定到临时对象(右值引用)的非引用,因此自然泛化随之而来,这就是您的技术所利用的。

于 2013-03-13T13:43:15.337 回答