template-meta-programming

Multi patterend varadic templates in C++

会有一股神秘感。 提交于 2019-12-24 07:18:18
问题 I don't think this is possible based on what I've read however I'm hoping someone here may know of some solution that would get this to work. I have a vector (maths) class for C++ template <typename T, size_t N> class vec; And want to create a varadic friend function apply to apply a function to these vectors element-wise i.e. template <typename F, typename ...Args> friend vec<typename std::result_of<pow(Args&&...)>::type, N> apply(F&& f, const vec<Args, N>&... args); which is valid (untested

C++ Template recursive to check type in std::tuple

南笙酒味 提交于 2019-12-24 06:35:58
问题 #include <iostream> #include <tuple> #include <type_traits> template<typename TupleType, typename T, std::size_t index = 0> constexpr std::size_t find_from(){ if constexpr (index == std::tuple_size_v<TupleType>) return index; if constexpr (std::is_same_v<std::tuple_element_t<index, TupleType>, T>) return index; return find_from<TupleType, T, index+1>(); } int main(){ std::cout << find_from<std::tuple<int,double>, int, 0>()<< std::endl; } I want to find the index of a type in a std::tuple, Why

Making sfinae works for functions with deduced return type?

六月ゝ 毕业季﹏ 提交于 2019-12-24 05:04:45
问题 Consider the following code: // -------------------------------------------------------------------------- // // Preprocessor #include <array> #include <vector> #include <utility> #include <iostream> #include <type_traits> // -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- // // Calls a function without arguments if it can be called template < class F, class... Args, class = decltype

C++ Create BSTR at compile time / insert length into string at compile time?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:24:39
问题 Is it possible using macro magic or TMP to insert the length into a string at compile time? For example: const wchar_t* myString = L"Hello"; I would want the buffer to actually contain "[length] [string constant]". I'm using MSVC 2010 which lacks constexpr. I figured there must be some trick to make this work as its possible to do: const wchar_t* myString = L"\x005Hello"; My attempt so far: template<int Size> wchar_t* toBstr(const wchar_t* str) { #pragma pack(push) #pragma pack(1) struct BStr

Use of enable_if to match only classes which have a certain static data member, and that has only specific values

最后都变了- 提交于 2019-12-24 01:03:11
问题 I want to specialize a function for a subset of classes which: have a certain static data member variable, and such variable has only certain possible values. The code below illustrates the intent, but it does not compile unless I comment out the lines related to the B classes in main . This is because code is not a member of the Bx classes, but the enable_if condition is valid if the template argument has a code member variables. How should I modify it? Unfortunately I work with very old

Meta-Function to get the first template of a list of templates

Deadly 提交于 2019-12-23 19:00:54
问题 Using the following metafunction front to get the first type of a typelist I try to write a similar metafunction to extract the first template of a list of templates. namescpace detail { template<typename L> struct front_impl; template<template<typename, typename...> typename L, typename F, typename... I> struct front_impl<L<F, I...>> { typedef F type; }; } template<typename L> struct front_impl; template<template<typename, typename...> typename L, typename F, typename... I> struct front_impl

Detecting instance method constexpr with SFINAE

家住魔仙堡 提交于 2019-12-23 17:27:21
问题 Let me first start off by noting the very similar question here: Detecting constexpr with SFINAE. The difference is that in this question, the detection method works to detect a static method of a class. I am trying to detect if something can be constexpr copy constructed. I'm quite close, here's what I've got: template <class T> constexpr int return_int(T t) { return 0; } template <class T> T& unmove(T&& t) { return t; } template <class ... T> using void_t = void; template <class T, class =

Understanding a “template argument is invalid” error message

隐身守侯 提交于 2019-12-23 15:34:17
问题 Consider the code: #include <type_traits> #include <iostream> struct test1 { void Invoke() {}; }; struct test2 { template<typename> void Invoke() {}; }; enum class InvokableKind { NOT_INVOKABLE, INVOKABLE_FUNCTION, INVOKABLE_FUNCTION_TEMPLATE }; template<typename Functor, class Enable = void> struct get_invokable_kind { const static InvokableKind value = InvokableKind::NOT_INVOKABLE; }; template<typename Functor> struct get_invokable_kind< Functor, decltype(Functor().Invoke()) > { const

Implementing static version of std::all_of using template metaprogramming?

孤街浪徒 提交于 2019-12-23 12:21:34
问题 Preface . I'm trying to get somewhat deeper understanding of C++ template metaprogramming and it seems, that I'm stuck... I'm writing a library, which we will use for binary data [de]serialization. The expected structure of data being unpacked is known to a certain extent and it seems reasonable for me to use this knowledge to (1) validate data (2) skip irrelevant parts and (3) unpack the data directly into structs known at compile-time - both for avoiding unnecessary copying and making the

How can I get the innermost template parameter type?

橙三吉。 提交于 2019-12-23 10:37:10
问题 Q In a dummy example of a class typedef myStruct<myStruct<myStruct<int>>> mv; int is the innermost template parameter. How can I get the type of that parameter for arbitrary nesting depth? Desired Result A mechanism to acquire the innermost type innermost<mv>::type -> int WishList Can this be done using template aliases (template template parameters are a missing feature here)? In an example where my type would be vector<vector<vector<int>>> Is there a way to perform the same operation, given