msvc is_copy_assignable always true?

后端 未结 2 574
南方客
南方客 2020-12-21 03:01
#include 

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

void fn(Test &a,          


        
相关标签:
2条回答
  • 2020-12-21 03:03

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

    0 讨论(0)
  • 2020-12-21 03:08

    May be a bit too late, but I have a workaround here: define another copy-and-move assignment operator, also as a deleted one. The compiler gives a warning, complaining about multiple copy operators (C4522), so you have to disable it for the class:

    #pragma warning (disable: 4522)
    class A
    {
        A& operator=(const A&) = delete;
        A& operator=(A) = delete;
    };
    #pragma warning (default: 4522)
    

    Now std::is_copy_assignable<A>::value is false.

    0 讨论(0)
提交回复
热议问题