c++14

Bind metafunction: accept both types and template template parameters (accept anything)

…衆ロ難τιáo~ 提交于 2019-12-04 15:30:57
I'm trying to write a Bind metaprogramming template helper metafunction that binds a template parameter to something. I have a working implementation for simple template metafunctions: template<typename T0, typename T1> struct MakePair { using type = std::pair<T0, T1>; }; template<template<typename...> class TF, typename... Ts> struct Bind { template<typename... TArgs> using type = TF<Ts..., TArgs...>; }; using PairWithInt = typename Bind<MakePair, int>::type; static_assert(std::is_same<PairWithInt<float>, MakePair<int, float>>{}, ""); But what if MakePair 's template arguments were template

Is there a way to iterate over at most N elements using range-based for loop?

回眸只為那壹抹淺笑 提交于 2019-12-04 14:59:17
问题 I would like to know if there is a nice way to iterate over at most N elements in a container using the range based for loop and/or algorithms from the standard library (that's the whole point, I know I can just use the "old" for loop with a condition). Basically, I'm looking for something that corresponds to this Python code: for i in arr[:N]: print(i) 回答1: As I personally would use either this or this answer (+1 for both), just for increasing your knowledge - there are boost adapters you

Clang >= 3.3 in c++1y mode cannot parse <cstdio> header

会有一股神秘感。 提交于 2019-12-04 14:57:48
问题 I have a project that correctly compiles and runs under g++ 4.8.1 and clang >= 3.3 in c++11 mode. However, when I switch to the experimental -std=c++1y mode, clang 3.3 (but not g++) chokes on the <cstdio> header that is indirectly included by way of Boost.Test (so I cannot easily change it myself) // /usr/include/c++/4.8/cstdio #include <stdio.h> // Get rid of those macros defined in <stdio.h> in lieu of real functions. // ... #undef gets // ... namespace std { // ... using ::gets; // <--

Is it good approach to “pass” function template as generic-variadic lambda return statement?

半城伤御伤魂 提交于 2019-12-04 14:29:49
I was having fun with attempts to pass function template as template template argument. Of course C++ doesn't allow passing function templates this way. But I came up with simple hax: #include <iostream> #define PASS_TEMPLATE(name) [](auto&... args){return name(args...);} template <typename T, typename S> void function_template(T t, S s) {std::cout << t << ' ' << s << std::endl;} template <typename Hax, typename T, typename S> auto test(Hax hax, T t, S s) { return hax(t, s); } int main() { test(PASS_TEMPLATE(function_template), 1, 1.5); } Demo The question is: Is this viable approach? (Is it

Correct usage of `for_each_arg` - too much forwarding?

瘦欲@ 提交于 2019-12-04 13:16:10
I'm really happy to have discovered for_each_arg(...) , which makes dealing with argument packs much easier. template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a),0)...}, f; } I'm, however, confused on its correct usage. There are many arguments that need to be perfectly forwarded, but am I performing any unnecessary forwarding? Reading the code becomes harder with excessive fowarding. struct UselessContainer { // Expects a perfectly-forwarded item to emplace template<typename T> void add(T&&) { } }; // Creates an `UselessContainer`

is returning a const std::string really slower than non-const?

柔情痞子 提交于 2019-12-04 13:04:09
In another question a user made a comment that returning a const std::string loses move construction efficiency and is slower. Is it really true that assigning a string of return of this method: const std::string toJson(const std::string &someText); const std::string jsonString = toJson(someText); ... is really slower than the non-const version: std::string toJson(const std::string &str); std::string jsonString = toJson(someText); And what is the meaning of move-construction efficiency in this context? I've never heard of that limitation before and do not remember having seen that in the

How to create a sorted mapping integer index with templates

拥有回忆 提交于 2019-12-04 12:57:58
问题 I have the data structure: template <int...I> struct index {}; template <typename...T> struct data {}; template <int I, int J> struct X { static constexpr int i = I; static constexpr int j = J; }; typedef data< X<0,4>, X<1,2>, X<2,1>, X<1,6>, X<1,3> > data_t; Where data does not contain duplicates and the indices J are small, in the range 0-31. I want to create a static index which contains the position in data of all X with index I equal to some given value (e.g. I=1), sorted by index J. It

Can we use the detection idiom to check if a class has a member function with a specific signature?

蓝咒 提交于 2019-12-04 12:43:30
Given a (reduced) implementation of the detection idiom namespace type_traits { template<typename... Ts> using void_t = void; namespace detail { template<typename, template<typename...> class, typename...> struct is_detected : std::false_type {}; template<template<class...> class Operation, typename... Arguments> struct is_detected<void_t<Operation<Arguments...>>, Operation, Arguments...> : std::true_type {}; } template<template<class...> class Operation, typename... Arguments> using is_detected = detail::is_detected<void_t<>, Operation, Arguments...>; template<template<class...> class

C++ The compiler is changing the alignment of my structures. How can I prevent this?

妖精的绣舞 提交于 2019-12-04 12:31:26
I am writing some code to read bitmap files. Here is the struct I am using to read the bitmap header. See also: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx struct BITMAPFILEHEADER { WORD bfType; // 2 DWORD bfSize; // 6 WORD bfReserved1; // 8 WORD bfReserved2; // 10 DWORD bfOffBits; // 14 }; // should add to 14 bytes If I put the following code in my main function: std::cout << "BITMAPFILEHEADER: " << sizeof(BITMAPFILEHEADER) << std::endl; the program prints: BITMAPFILEHEADER: 16 It appears to be re-aligning the data in the struct on 4-byte boundaries,

Given a set of classes, call the one with matching method parameters

人走茶凉 提交于 2019-12-04 12:15:21
I have 2 or more classes which inherit from a single parent. They all have overloaded handle methods, but each class has different parameters for their handle methods. class CommandHandler {}; class FooCommandHandler : public CommandHandler { public: string handle(const FooCommand& c) { return c.getStringSomehow(); } }; class BarCommandHandler : public CommandHandler { public: string handle(const BarCommand& c) { return c.getStringSomeOtherWay(); } string handle(const OtherBarCommand& c) { return c.youGetThePicture(); } }; FooCommandHandler fooHandler; BarCommandHandler barHandler; I want a