template-meta-programming

Create n-dimensional vector with given sizes

陌路散爱 提交于 2019-12-18 04:10:55
问题 So, what I want is to create multidimensional vector of given type where the first dimension will have size of the first argument of a function call, etc, for example if I do std::size_t n = 5; auto x = make_vector<int>(n + 1, n * 2, n * 3); x should be 6x10x15 3d array (consisting of zeroes, because I want to default construct right now) I have tried this: template <typename T> std::vector<T> make_vector(std::size_t size) { return std::vector<T>(size); } template <typename T, typename...

Create n-dimensional vector with given sizes

回眸只為那壹抹淺笑 提交于 2019-12-18 04:10:01
问题 So, what I want is to create multidimensional vector of given type where the first dimension will have size of the first argument of a function call, etc, for example if I do std::size_t n = 5; auto x = make_vector<int>(n + 1, n * 2, n * 3); x should be 6x10x15 3d array (consisting of zeroes, because I want to default construct right now) I have tried this: template <typename T> std::vector<T> make_vector(std::size_t size) { return std::vector<T>(size); } template <typename T, typename...

Possible to use type_traits / SFINAE to find if a class defines a member TYPE?

流过昼夜 提交于 2019-12-18 00:42:10
问题 I have seen this question which allows one to check for the existence of a member function , but I'm trying to find out whether a class has a member type . In the example below, both evaluate to "false", but I would like to find a way so that has_bar<foo1>::value evaluates to false, and has_bar<foo2>::value evaluates to true . Is this possible? #include <iostream> struct foo1; struct foo2 { typedef int bar; }; template <typename T> class has_bar { typedef char yes; typedef long no; template

detecting typedef at compile time (template metaprogramming)

房东的猫 提交于 2019-12-17 23:32:46
问题 I am currently doing some template metaprogramming. In my case I can handle any "iteratable" type, i.e. any type for which a typedef foo const_iterator exists in the same manner. I was trying to use the new C++11 template metaprogramming for this, however I could not find a method to detect if a certain type is missing. Because I also need to turn on/off other template specializations based on other characteristics, I am currently using a template with two parameters, and the second one gets

c++ power of integer, template meta programming

假装没事ソ 提交于 2019-12-17 18:07:12
问题 I want to make a function which returns a power of integer. Please read the fmuecke's solution in power of an integer in c++ . However, I want to generalize his solution to the arbitrary type T. Since c++11 has constexpr, I guess this is possible. Naively, I tried something like, template<class T, int N> inline constexpr T pow(const T x){ return pow<N-1>(x) * x; } template<class T> inline constexpr T pow<T, 1>(const T x){ return x; } template<class T> inline constexpr T pow<T, 0>(const T x){

details of std::make_index_sequence and std::index_sequence

烂漫一生 提交于 2019-12-17 10:46:18
问题 I'm enjoying ramping up on variadic templates and have started fiddling about with this new feature. I'm trying to get my head around the implementation details of std::index_sequence 's (used for tuple implementation). I see sample code around there, but I really want a dumbed down step by step explanation of how an std::index_sequence is coded and the meta programming principal in question for each stage. Think really dumbed down :) 回答1: I see sample code around there, but I really want a

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

不羁岁月 提交于 2019-12-17 09:52:42
问题 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*

Deducing first template argument with other template parameters defaulted

元气小坏坏 提交于 2019-12-17 09:43:37
问题 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

C++ define a class member type according to its template

狂风中的少年 提交于 2019-12-14 04:18:28
问题 In our code we have the following class: template<class A, class B> class myClass { typedef std::list<Object<A,B>> MyList; typedef std::map<A, typename mList::iterator> MyMap MyList mList; MyMap mMap; } class A is metaprogrammed and it can be a string, int and so on. I would like to change the code so in case class A is a "meta string" a map will be used, otherwise unordered_map will be used. I've tried to add some more meta programming but haven't succeeded yet: template< class A, class B>

Prevent header from being included in some files, in compilation time?

醉酒当歌 提交于 2019-12-14 04:17:55
问题 I have a header file, which I can control its contents. Additionally I have an interface I1 (defined in some other file) from which various implementations derived. I want to prohibit those implementations from including this header file. So that during compile time if the file is included the compilation will fail, otherwise it will continue as usual. So I have header file and an interface definition (in some other file). I want to prohibit interface implementations from including the given