右值是新的,所以我对此有些怀疑。

VS2013给

warning C4172: returning address of local variable or temporary


对于这条线

return (std::wstring&&)         (*(std::wstring*)v);


该代码(下面)是否不可移植和/或在任何地方具有未定义的行为?如果有-在哪里,为什么?难道我做错了什么?
还是这只是VS2013编译器的假阳性警告,在这种特殊情况下,我应该放一些实用程序以忽略它?

temp_value_t旨在用作一系列调用的容器:

[类型值]->调用virtual_func1(temp_value_t)->调用virtual_func2(temp_value_t)-> ...->调用virtual_funcLAST(temp_value_t)

并且funcLAST实现知道temp_value_t是真正的右值还是左值,因此funcLAST知道应使用哪个函数。

#include "stdafx.h"

class temp_value_t
    {
    private:
        intptr_t v;
    public:
        temp_value_t(const std::wstring& s)
            {
            v = (intptr_t)&s;
            };

        temp_value_t(std::wstring&& s)
            {
            v = (intptr_t)&s;
            };

        std::wstring&& get_as_temp()
            {
            return (std::wstring&&)         (*(std::wstring*)v);
            };

        const std::wstring& get_as_const()
            {
            return (const std::wstring&)    (*(std::wstring*)v);
            };
    };

void test(temp_value_t v, bool is_param_const)
    {
    std::wstring s;

    if(is_param_const)
        s = v.get_as_const();
    else
        s = v.get_as_temp();

    std::wcout <<  L"Inner = '" << s << L"'\n";
    };

int _tmain(int argc, _TCHAR* argv[])
    {
    {
    std::wcout <<  L"Test with temp:\n";
    std::wstring s(L"abc");
    test(s, false);
    std::wcout <<  L"Outer = '" << s << L"'\n";
    }

    std::wcout <<  L"\n\n";

    {
    std::wcout <<  L"Test with const:\n";
    std::wstring s(L"abc");
    test(s, true);
    std::wcout <<  L"Outer = '" << s << L"'\n";
    }

    return 0;
    }

/*
VS2013 results:
    Test with temp:
    Inner = 'abc'
    Outer = ''


    Test with const:
    Inner = 'abc'
    Outer = 'abc'

So all works fine...
*/

最佳答案

为什么不存储给定字符串的引用?

class temp_value_t
{
    private:
        const std::wstring & v;
    public:
        temp_value_t(const std::wstring & s)
            : v(s)
        {
        }

        // You can't store reference if you are expecting an rvalue.
        // Because, that rvalue is going to or may expire soon,
        // and when that happens, the stored reference will be invalid.
        //temp_value_t(std::wstring && s)
        //  : v(std::move(s))
        //{
        //}

        // This will already return an rvalue.
        // It is meaningless to cast it to (std::wstring&&).
        std::wstring get_as_temp()
        {
            return v;
        }

        const std::wstring & get_as_const()
        {
            return v;
        }
};


如果您的目标是存储作为右值引用传递的字符串对象,请定义移动构造函数,然后将右值字符串存储在对象内部。

10-01 22:38