0
我使用的是模板的構造在我班的一個模板構造函數失去右值
有誰告訴我爲什麼這個代碼,沒有警告或錯誤以及代碼(創建默認FOO右值測試),但一個行編譯剛消失了!
#include <iostream>
class Foo
{
int _value;
public:
Foo()
:_value(0)
{
std::cout << __PRETTY_FUNCTION__ << "value[" << _value << "]" << std::endl;
}
Foo(int value)
:_value(value)
{
std::cout << __PRETTY_FUNCTION__ << "value[" << _value << "]" << std::endl;
}
Foo(Foo&& foo) = delete;
Foo(const Foo& foo) = delete;
friend std::ostream& operator << (std::ostream& output, const Foo& foo)
{
output << foo._value;
return output;
}
};
class Test
{
public:
template <typename Type>
Test(Type&& value)
{
std::cout << __PRETTY_FUNCTION__ << " with value[" << value << "]" << std::endl;
}
template <typename Type>
void fn(Type&& value)
{
std::cout << __PRETTY_FUNCTION__ << " with value[" << value << "]" << std::endl;
}
};
int main()
{
std::cout << "//----- test fn with foo rvalue ---------------" << std::endl;
Test test3(3);
test3.fn(Foo());
std::cout << "//----- test with int rvalue ------------------" << std::endl;
Test test4(1+3);
std::cout << "//----- create test with default foo rvalue ---" << std::endl;
Test test5(Foo());
std::cout << "//----- create test with foo rvalue -----------" << std::endl;
Test test7 (Foo(1+6));
std::cout << "//----- create test with moved foo rvalue -----" << std::endl;
Test test8(std::move(Foo()));
return 0;
}
這將產生以下結果
//----- test fn with foo rvalue ---------------
Test::Test(Type&&) [with Type = int] with value[3]
Foo::Foo()value[0]
void Test::fn(Type&&) [with Type = Foo] with value[0]
//----- test with int rvalue ------------------
Test::Test(Type&&) [with Type = int] with value[4]
//----- create test with default foo rvalue ---
//----- create test with foo rvalue -----------
Foo::Foo(int)value[7]
Test::Test(Type&&) [with Type = Foo] with value[7]
//----- create test with moved foo rvalue -----
Foo::Foo()value[0]
Test::Test(Type&&) [with Type = Foo] with value[0]
我使用克++(Ubuntu的4.8.2-19ubuntu1)4.8.2與STD = C++ 1Y
如果有幫助,我添加了一行在端
std::cout << test5;
而編譯器產生一個警告
warning: the address of ‘Test test5(Foo (*)())’ will always evaluate as ‘true’ [-Waddress]
------------------ – 2014-10-31 12:01:14