msvc is_copy_assignable 始终为真?

2021-12-31 00:00:00 visual-c++ c++ c++11 visual-studio-2013
#include <type_traits>

class Test
{
public:
    Test(const Test &) = delete;
    Test &operator=(const Test &) = delete;
};

void fn(Test &a, const Test &b) { a = b; }

static_assert(!std::is_copy_assignable<Test>::value, "Test shouldn't be assignable");

在 MSVC 2013 Update 3 下编译它意外地使 static_assert 失败,并且函数 fn 无法编译(如预期).这是矛盾的,对吧?

Compiling this under MSVC 2013 Update 3 unexpectedly fails the static_assert, and the function fn fails to compile (as expected.) This is contradictory, right?

我是否滥用了is_copy_assignable?有没有其他方法可以测试这种情况?

Am I misusing is_copy_assignable? Is there another way to test for this condition?

推荐答案

您说得对,这是一个错误:https://connect.microsoft.com/VisualStudio/feedback/details/819202/std-is-assignable-and-std-is-constructible-give-wrong-value-for-deleted-members

You are correct this is a bug: https://connect.microsoft.com/VisualStudio/feedback/details/819202/std-is-assignable-and-std-is-constructible-give-wrong-value-for-deleted-members

我拿了 cplusplus.com 的 is_copy_assignable 代码:

I took cplusplus.com's is_copy_assignable code:

#include <iostream>
#include <type_traits>

struct A { };
struct B { B& operator= (const B&) = delete; };

int main() {
    std::cout << std::boolalpha;
    std::cout << "is_copy_assignable:" << std::endl;
    std::cout << "int: " << std::is_copy_assignable<int>::value << std::endl;
    std::cout << "A: " << std::is_copy_assignable<A>::value << std::endl;
    std::cout << "B: " << std::is_copy_assignable<B>::value << std::endl;
    return 0;
}

并在 Visual Studio 2013 上对其进行测试并得到:

And tested it on Visual Studio 2013 and got:

is_copy_assignable:
整数:真
答:是的
乙:真的

is_copy_assignable:
int: true
A: true
B: true

在 gcc 4.8.1 上,我得到了:

is_copy_assignable:
整数:真
答:是的
乙:假

is_copy_assignable:
int: true
A: true
B: false

特别是在 Visual Studio 2015 Beta 上,此问题已修复.我得到:

Notably on the Visual Studio 2015 Beta this is fixed. I get:

is_copy_assignable:
整数:真
答:是的
乙:假

is_copy_assignable:
int: true
A: true
B: false

你对测试版的感觉如何;)

How do you feel about betas ;)

相关文章