I understand that after a typename, a single &
means an lvalue reference, and a double &&
means an rvalue reference or a "deduced reference", also called a universal reference by Scott Meyers. But I have never seen a triple &&&
in a function or method signature. What does it stand for?
The following code produces a method with triple &&&
:
#include <iostream>
#include <utility>
template<typename ostream> void foo(ostream&& out) {
out << "foo" << std::endl;
}
template<typename ostream> void bar(ostream&& out) {
foo(std::forward<ostream>(out));
}
int main() {
bar(std::cout);
}
After compiling the code with g++-4.8.1
, I run nm -j a.out | c++filt
. The -j
switch is (I believe) nonstandard, it means just display the symbol names (no value or type). I get this:
__GLOBAL__sub_I_triple_ampersand.cpp
void bar<std::ostream&>(std::ostream&&&)
void foo<std::ostream&>(std::ostream&&&)
__static_initialization_and_destruction_0(int, int)
std::ostream::operator<<(std::ostream& (*)(std::ostream&))
std::ios_base::Init::Init()
std::ios_base::Init::~Init()
std::cout
std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
std::ostream&&& std::forward<std::ostream&>(std::remove_reference<std::ostream&>::type&)
std::piecewise_construct
std::__ioinit
std::basic_ostream<char, std::char_traits<char> >& std::operator<<<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
___cxa_atexit
__mh_execute_header
_main
dyld_stub_binder
I get similar output when I compile with clang++
. So my question is, what does the triple ampersand stand for? Apparently I cannot write &&&
in my code directly. Is it because my c++filt
is demangling the symbols incorrectly? I am using system provided c++filt
on Mac OS X 10.8.