Code Monkey home page Code Monkey logo

Comments (4)

hmenke avatar hmenke commented on May 24, 2024 1

The code on cppreference.com is just copied from the corresponding C++ Standard proposal, but the code itself is actually much older than that and has been floating around on the internet for years. I don't even know who the original author is and would just assume that the code is public domain.

BTW, the CC-BY-SA licensing of cppreference.com is only a protection against someone just copying the whole wiki and filling it up with ads to make money from it.

from dbg-macro.

hmenke avatar hmenke commented on May 24, 2024 1

Just to give you some reassurance, libstdc++ has the exact same code in the standard library, just with the variables renamed:
https://github.com/gcc-mirror/gcc/blob/b05c7e439a823d99781e4aaa47a9782fa6693cb5/libstdc%2B%2B-v3/include/std/type_traits#L2582-L2598
https://github.com/gcc-mirror/gcc/blob/b05c7e439a823d99781e4aaa47a9782fa6693cb5/libstdc%2B%2B-v3/include/experimental/type_traits#L228-L250

from dbg-macro.

hmenke avatar hmenke commented on May 24, 2024

I recommand is_detected for that.

#include <type_traits>

// -- begin is_detected
//    https://en.cppreference.com/w/cpp/experimental/is_detected

struct nonesuch {
    ~nonesuch() = delete;
    nonesuch(nonesuch const&) = delete;
    void operator=(nonesuch const&) = delete;
};

namespace detail {

template <typename ...>
using void_t = void;

template <class Default, class AlwaysVoid,
          template<class...> class Op, class... Args>
struct detector {
  using value_t = std::false_type;
  using type = Default;
};
 
template <class Default, template<class...> class Op, class... Args>
struct detector<Default, void_t<Op<Args...>>, Op, Args...> {
  using value_t = std::true_type;
  using type = Op<Args...>;
};
 
} // namespace detail
 
template <template<class...> class Op, class... Args>
using is_detected = typename detail::detector<nonesuch, void, Op, Args...>::value_t;
 
template <template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;
 
template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;

// -- end is_detected

template <typename T>
using detect_begin_t = decltype(begin(std::declval<T>()));

template <typename T>
using detect_end_t = decltype(end(std::declval<T>()));

template <typename T>
using detect_size_t = decltype(std::declval<T>().size());

template <typename T>
struct has_begin_end_size {
    static constexpr bool value = is_detected<detect_begin_t, T>::value
                                  && is_detected<detect_end_t, T>::value
                                  && is_detected<detect_size_t, T>::value;
};

template <typename Container>
typename std::enable_if<has_begin_end_size<Container>::value, bool>::type
prettyPrint(std::ostream &stream, Container const &value) {
    // ...
}

from dbg-macro.

sharkdp avatar sharkdp commented on May 24, 2024

Thank you very much!

I didn't know is_detected. That seems like a very useful "wrapper" around std::void_t.

Ideally, I would like to keep dbg.h C++11 compatible, so we would have to include an implementation of void_t as well.

(a technical thing: the content on cppreference.com seems to be CC-BY-SA licensed. I'm not really familiar with the details, but I don't think it's compatible with MIT)

from dbg-macro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.