Showing posts with label cpp_std. Show all posts
Showing posts with label cpp_std. Show all posts

Oct 12, 2022

[C++23] std::unreachable / gcc::__builtin_unreachable

std::unreachable

If control flow reaches the point of the std::unreachable, the program is undefined. It is useful in situations where the compiler cannot deduce the unreachability of the code.


Reference:

Nov 28, 2018

[C++][cppcon 2018] std::basic_string: for more than just text - Brian Ruth


This could be one of the most interesting/hacking videos in cppcon 2018 :-D

std::basic_string can be the container other then 'char'-ish type.
i.e int, bool, UDT etc.

And, it applies SSO on those types as well, thus we might ask, why vector
doesn't have SSO?
Well, we do have in LLVM:
SmallVector.h
or Boost's:
small_vector

And we might have 'relocatable' in C++20 iff vector contains type that meets Rule Of Zero.



Sep 29, 2016

[C++11] shared_ptr alias constructor

std::shared_ptr's secret constructor


struct X{
    Y y;
};


struct do_nothing_deleter{
    template<typename> void operator()(T*){}
};


void store_for_later(std::shared_ptr<Y>);


void foo(){
    std::shared_ptr<X> px(std::make_shared<X>());
    std::shared_ptr<Y> py(&px->y,do_nothing_deleter());
    store_for_later(py);
} // our X object is destroyed, BAD!
void bar(){
    std::shared_ptr<X> px(std::make_shared<X>());
    std::shared_ptr<Y> py(px,&px->y);
    store_for_later(py);
} // our X object is kept alive, i.e px NEVER destroyed even out of scope.


struct X2{
    std::unique_ptr<Y> y;
    X2():y(new Y){}
};


void baz(){
    std::shared_ptr<X2> px(std::make_shared<X2>());
    std::shared_ptr<Y> py(px,px->y.get());
    store_for_later(py);
} // our X2 object is kept alive, DITTO
aa as

http://stackoverflow.com/a/27109774/1316609
However, r-reference to prvalue's data member will exist
even the prvalue destructs the internal data member(i.e r-reference make a copy out of it),
same as const T&.

For const T& to a l-value data member, the destruct of internal data member will
effect the outer const T&, this is by standard.
http://stackoverflow.com/a/3097861

Sep 22, 2016

[c++] addressof implement detail

possible std::addressof implement
http://en.cppreference.com/w/cpp/memory/addressof


template<class T>
struct addr_impl_ref
{
  T & v_;

  inline addr_impl_ref( T & v ): v_( v ) {}
  inline operator T& () const { return v_; }

private:
  addr_impl_ref & operator=(const addr_impl_ref &);
};

template<class T>
struct addressof_impl
{
  static inline T * f( T & v, long ) {
    return reinterpret_cast<T*>(
        &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
  }

  static inline T * f( T * v, int ) { return v; }
};

template<class T>
T * addressof( T & v ) {
  return addressof_impl<T>::f( addr_impl_ref<T>( v ), 0 );
}