Skip to main content
Code Review

Return to Answer

remove non-nice sentence
Source Link
L. F.
  • 9.7k
  • 2
  • 27
  • 69

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659 N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

    
     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
      class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
    
  2. [any.bad_any_cast]/2[any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

And surprisingly, I can't find any more mistakes!

Other suggestions (priority: low)

  1. [any.class]/3[any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is meaninglessunnecessary:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

    
     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
     
  2. [any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

And surprisingly, I can't find any more mistakes!

Other suggestions (priority: low)

  1. [any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is meaningless:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
    
  2. [any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

Other suggestions (priority: low)

  1. [any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is unnecessary:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

a static_assert doesn't "execute"
Source Link
L. F.
  • 9.7k
  • 2
  • 27
  • 69

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
     
  2. [any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

And surprisingly, I can't find any more mistakes!

Other suggestions (priority: low)

  1. [any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is never executedmeaningless:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
     
  2. [any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

And surprisingly, I can't find any more mistakes!

Other suggestions (priority: low)

  1. [any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is never executed:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
     
  2. [any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

And surprisingly, I can't find any more mistakes!

Other suggestions (priority: low)

  1. [any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is meaningless:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

Source Link
L. F.
  • 9.7k
  • 2
  • 27
  • 69

Your implementation is excellent! I can hardly find any problems. I was amazed how simple a conforming implementation of any can be. And I wholeheartedly agree with @papagaga's comment.

Here's my two cents. I use the N4659, the C++17 final draft, as a reference.

Non-conformance (priority: high)

  1. Thou Shalt Not Specialize std::swap. Instead, you should overload swap to be found by ADL. See How to overload std::swap() on Stack Overflow.

     class any {
     public:
     // ...
     friend void swap(any& lhs, any& rhs)
     {
     lhs.swap(rhs);
     }
     };
     
  2. [any.bad_any_cast]/2 specifies that bad_any_cast should derive from std::bad_cast. Your implementation fails to do this.

And surprisingly, I can't find any more mistakes!

Other suggestions (priority: low)

  1. [any.class]/3 says:

    Implementations should avoid the use of dynamically allocated memory for a small contained value. [ Example: where the object constructed is holding only an int. — end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v<T> is true.

    Clearly, you did not implement this optimization.

  2. Initially I thought, "where is your destructor?" Then I realized that the synthesized destructor is equivalent to reset(). I recommend you explicitly default this to reduce confusion since you implemented the rest of the Big Five.

     ~any() = default;
    
  3. The following static_assert on line 40 is never executed:

     static_assert(std::is_copy_constructible_v<std::decay_t<ValueType>>, "program is ill-formed");
    

    because this constructor does not participate in overload resolution unless std::is_copy_constructible_v<std::decay_t<ValueType>>.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /