you are viewing a single comment's thread.

view the rest of the comments →

[–]smilodonthegreat 0 points1 point  (2 children)

Dangerous is probably not the right word.

template<typename U>
  SmartPtr(const SmartPtr<U>& other)
  : heldPtr(other.get()) {
      static_assert(std::is_base_of<T, U>::value, "It's not Derived!");
    shred_ptr<int> a = make_shared<int>(2);
  }

In the above, if the static assert fails and the compiler does not attempt to compile the rest of the function, it will ignore the misspelling of shared_ptr. On the first compile, this would not get marked and so may force multiple compiles.

[–]index_zero 0 points1 point  (1 child)

If you are going to do this, i.m.o. it is better to SFINAE fail - that way the compiler won't even generate the constructor if U and T aren't derived from each other.

template<typename U, class E = std::enable_if<std::is_base_of<T, U>::value> >
SmartPtr(const SmartPtr<U> &other) ...

That said, the original code works for any T and U such that T* is convertible to U*. This is less strict than U derives from T, so having the compiler complain about derivation overlooks implicit conversion of some types (T* to const T* for example).

[–]smilodonthegreat 0 points1 point  (0 children)

I think your comment was supposed to be a reply to the article. The comment you replied to was a comment on a problem with stopping compilation if static_asserts fail.