相关疑难解决方法(0)

std :: move of string literal - 哪个编译器正确?

给出以下代码:

#include <string>
void foo()
{
  std::string s(std::move(""));
}
Run Code Online (Sandbox Code Playgroud)

这与apple clang(xcode 7)编译,而不是与visual studio 2015生成以下错误:

error C2440: 'return': cannot convert from 'const char [1]' to 'const char (&&)[1]'
note: You cannot bind an lvalue to an rvalue reference
main.cpp(4): note: see reference to function template instantiation 'const char (&&std::move<const char(&)[1]>(_Ty) noexcept)[1]' being compiled
    with
    [
        _Ty=const char (&)[1]
    ]
Run Code Online (Sandbox Code Playgroud)

暂时忽略移动是多余的,在这种情况下哪个标准库实现更正确?

我的感觉是该类型即""const char[1]如此std::move应该返回std::remove_reference<const char[1]&>::type&&这将是const char[1]&&.

在我看来,这应该衰败const char*.

或者我是否误解了这些规则?

c++ language-lawyer visual-studio-2015

27
推荐指数
1
解决办法
1116
查看次数

根据经验确定C++ 11表达式的值类别?

C++ 11中的每个表达式都有一个值类别.lvalue,xvalue或prvalue之一.

有没有办法编写一个宏,给定任何表达式作为参数,将产生一个字符串"lvalue","xvalue"或"prvalue"酌情?

例如:

int main()
{
    int x;

    cout << VALUE_CAT(x) << endl; // prints lvalue
    cout << VALUE_CAT(move(x)) << endl; // prints xvalue
    cout << VALUE_CAT(42) << endl; // prints prvalue
}
Run Code Online (Sandbox Code Playgroud)

怎么可以VALUE_CAT实现?

c++ c++11

25
推荐指数
1
解决办法
1007
查看次数

非const引用绑定到临时,Visual Studio错误?

我在编译一些可移植代码时碰到了这个gcc.基本上这个奇怪的代码在Visual Studio中编译,这真的让我大吃一惊:

class Zebra {int x;};
Zebra goo() {Zebra z; return z;}
void foo(Zebra &x)
{
    Zebra y;
    x = y;
    foo(goo());
}
Run Code Online (Sandbox Code Playgroud)

Visual studio让这一个飞.gcc将捕获此作为编译错误.有趣的是,如果你输入def Zebra为int,VC++会抱怨.相当矛盾的行为.思考?

c++ visual-studio temporary-objects

23
推荐指数
2
解决办法
2611
查看次数

施放的结果是左值?

int a = 0;
Run Code Online (Sandbox Code Playgroud)

那么是标准C++中(int)a右值

不同的编译器显示此代码的不同结果:

#include <iostream>
using namespace std;

void f(int& x)
{
    cout << "l value" << endl;
}

void f(int&& x)
{
    cout << "r value" << endl;
}

int main()
{
    int a = 0;
    f((int)a);
}
Run Code Online (Sandbox Code Playgroud)

编译器有不同的结果:

1)http://cpp.sh/2r6

2)http://webcompiler.cloudapp.net/

c++ standards casting lvalue visual-c++

15
推荐指数
3
解决办法
1174
查看次数