template-meta-programming

How to check if two types are same at compiletime(bonus points if it works with Boost strong typedef)

十年热恋 提交于 2019-11-27 11:37:15
问题 I was wondering if it is possible to check if 2 types are same at compile time. What I came up with is(idk if it works because it feels hackish and IDK standard that good so IDK what to look for when testing). #include <boost/strong_typedef.hpp> BOOST_STRONG_TYPEDEF(double, cm); BOOST_STRONG_TYPEDEF(double, inch); template<typename T, typename U> static constexpr void __help() { } template<typename T, typename U> class AreSameType { public: constexpr operator bool() { return &__help<T,U> == &

How can I detect if a type can be streamed to an std::ostream?

依然范特西╮ 提交于 2019-11-27 10:20:09
问题 I'm trying to write a type trait to detect if a type has overloaded operator<<() suitable to use to an output stream. I'm missing something because I'm always getting true for a simple empty class with no operators at all. Here the code: template<typename S, typename T> class is_streamable { template<typename SS, typename TT> static auto test(SS&& s, TT&& t) -> decltype(std::forward<SS>(s) << std::forward<TT>(t)); struct dummy_t {}; static dummy_t test(...); using return_type = decltype(test

C++11 “overloaded lambda” with variadic template and variable capture

耗尽温柔 提交于 2019-11-27 09:06:40
I'm investigating a C++11 idiom which might be called "overloaded lambda": http://cpptruths.blogspot.com/2014/05/fun-with-lambdas-c14-style-part-2.html http://martinecker.com/martincodes/lambda-expression-overloading/ Overloading n functions with variadic template seemed very appealing to me but it turned out it didn't work with variable capture: any of [&] [=] [y] [&y] (and [this] etc if in a member function) lead to compilation failure: error: no match for call to '(overload<main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)> >) (char*&)' (with my local GCC 4.9.1 and ideone

Deducing first template argument with other template parameters defaulted

爷,独闯天下 提交于 2019-11-27 08:40:54
Gcc and clang seem to disagree on whether this code should compile or not: #include <type_traits> template <typename Signature, int N = 0> struct MyDelegate { }; template <typename D> struct signature_traits; template <template <typename> class Delegate, typename Signature> struct signature_traits<Delegate<Signature>> { using type = Signature; }; static_assert(std::is_same_v< void(int, int), signature_traits<MyDelegate<void(int, int)>>::type >); See godbolt output here and try it . I'm siding with clang here, but what does the C++ standard say about this? A follow-up question - can this be

void_t “can implement concepts”?

佐手、 提交于 2019-11-27 05:59:36
I was watching the second part of Walter Brown's CppCon2014 talk on template metaprogramming , during which he discussed the uses of his novel void_t<> construction. During his presentation Peter Sommerlad asked him a question that I didn't quite understand. (link goes directly to the question, the code under discussion took place directly before that) Sommerlad asked Walter, would that mean we actually can implement concepts lite right now? to which Walter responded Oh yeah! I've done it ... It doesn't have quite the same syntax. I understood this exchange to be about Concepts Lite. Is this

Apply the first valid function of a set of N functions

吃可爱长大的小学妹 提交于 2019-11-27 03:30:24
问题 This previous answer shows how to apply function based on the validity of a call: here. However, it applies to two functions. I was wondering if the concept could be generalized to N functions using smart template programming tricks, in C++14. The problem is the following: template <std::size_t N, class... X> /* [Return type] */ apply_on_validity(X&&... x) { // Tricks here } // The function would be equivalent to a hypothetical // template <std::size_t N, class... F, class... Args> // [Return

“What happened to my SFINAE” redux: conditional template class members?

房东的猫 提交于 2019-11-27 02:59:20
问题 I'm new to writing template metaprogramming code (vs. just reading it). So I'm running afoul of some noob issues. One of which is pretty well summarized by this non-SO post called "What happened to my SFINAE?", which I will C++11-ize as this: (Note: I gave the methods different names only to help with my error diagnosis in this "thought experiment" example. See @R.MartinhoFernandes's notes on why you wouldn't actually choose this approach in practice for non-overloads.) #include <type_traits>

C++ construct that behaves like the __COUNTER__ macro [duplicate]

拟墨画扇 提交于 2019-11-27 02:58:04
问题 This question already has an answer here: Does C++ support compile-time counters? 7 answers I have a set of C++ classes and each one must declare a unique sequential id as a compile-time constant. For that I'm using the __COUNTER__ built-in macro which translates to an integer that is incremented for every occurrence of it. The ids need not to follow a strict order. The only requirement is that they are sequential and start from 0: class A { public: enum { id = __COUNTER__ }; }; class B {

Automatically count the number of instantiated classes in a TMP?

女生的网名这么多〃 提交于 2019-11-27 02:17:51
问题 Given a template metaprogram (TMP), do C++ compilers produce build statistics that count the number of instantiated classes? Or is there any other way to automatically get this number? So for e.g. the obiquitous factorial #include <iostream> template<int N> struct fact { enum { value = N * fact<N-1>::value }; }; template<> struct fact<1> { enum { value = 1 }; }; int main() { const int x = fact<3>::value; std::cout << x << "\n"; return 0; } I would like to get back the number 3 (since fact<3>,

Get argument type of template callable object

感情迁移 提交于 2019-11-27 02:10:09
问题 Consider the following function: template<class F> void register_handler( F& f ) // any callable object { // find out T - the argument type of f } Here f is some callable object, accepting one argument. It may be a function pointer, an std::function or a result of std::bind . The problem is, how to determine the argument type of f and do some actions based on that type? An easy workaround would be to add the type to template explicitly, like template<class T, class F> // T is the argument