Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

c++ - msvc is_copy_assignable always true?

#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");

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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;
}

And tested it on Visual Studio 2013 and got:

is_copy_assignable:
int: true
A: true
B: true

On gcc 4.8.1 I got:

is_copy_assignable:
int: true
A: true
B: false

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

is_copy_assignable:
int: true
A: true
B: false

How do you feel about betas ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...