bitmask
bitmask

Reputation: 34644

Why is a struct with const members assignable?

I needed to use the std::is_assignable type traits, which happens not to be available in the oldest compiler version that I want to support. Since I had to implement this myself (okay, I admit it, there was some interwebs peeking), I now wonder if this is a bug in my implementation or a general problem of std::is_assignable.

First of all, this is my test struct:

struct Bar {
  bool const cb; // this should kill the default assignment operator
  int i;
};

and here is my stdreplace::is_assignable

template <typename T> struct is_assignable : private std::__sfinae_types {
  private:
    template <typename T1> 
      static decltype(std::declval<T1>() = std::declval<T1>(),__one()) test(int);
    template <typename T1> 
      static __two test(...);
  public:
    static bool const value = sizeof(test<T>(0)) == sizeof(__one);
};

However, it tells me, Bar is assignable:

int: 1
Bar: 1
int[2]: 0

Where is the problem?

Upvotes: 2

Views: 255

Answers (1)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507215

Your compiler probably does not correctly define the assignment operator as deleted. In c++03 code with respect to implicitly declared special member functions, such an assignment within an unevaluated operand was fine even if the assignment would be illformed if it occurs in an evaluated operand. The reason for that is because the asignment operator would never be implicitly defined but only implicitly declared thus never rising an error.

Since that changed in c++11 and your assignment operator should be deleted automatically it appears to me that your compiler is at fault.

Upvotes: 3

Related Questions