c++14

Type-only template argument to lambda

折月煮酒 提交于 2019-12-12 11:05:02
问题 Imagine I've got this struct: struct Foo { operator int() { return 11; } operator unsigned int() { return 22; } } foo; When this struct is casted to an int, it returns 11, but when casted to an unsigned int, it returns 22. Using normal functions, I could use templates and a getter function to choose: template<typename T> T get() { return (T)foo; } Now, when calling this function like get<int>() it would return 11 , but when calling it like get<unsigned int>() it would return 22 . Everything's

Why does the following program not select the argument of the same type as the first template parameter?

依然范特西╮ 提交于 2019-12-12 10:45:41
问题 I am trying to write a function such that f<T>(args..) returns the first parameter of type T . The following program seems to always select the first specialization thus printing 97 (ASCII code of 'a' ). Though the second one wouldn't require converting char to int . Could someone please explain the behavior? I am new to SFINAE and meta-programming. #include <iostream> using namespace std; template <typename T, typename ...Ts> T f(T a, Ts... args) { return a; } template <typename R, typename

Is it practically OK to delete object not constructed using the new expression?

荒凉一梦 提交于 2019-12-12 10:39:09
问题 Pedantically, this may not be OK. As per cppref: If expression is anything else, including if it is a pointer obtained by the array form of new-expression, the behavior is undefined. Putting that aside, is the following code OK in practice ( T is non-array, and assuming that new is not replaced)? auto p = (T*)operator new(sizeof(T)); new(p) T{}; delete p; It is said that in cppref that When calling the allocation function, the new-expression passes the number of bytes requested as the first

Change array dimensions at runtime

落花浮王杯 提交于 2019-12-12 09:56:39
问题 I have this matrix class which has a 2d double array. In the constructor you can specify the width and height. I want to create a 1d array instead of a 2d when the width is 1. Because I overloaded the [] operator and return the pointer. If there is only 1 row/col I don't want to always write [i][0]. Instead I want to just write [i]. Does anyone know how to solve this? Edit: To clarify this I need this class for matrix calculations no just as array. 回答1: I would recommend using a one

how to improve performance of boost::spirit::x3 key-value parser

情到浓时终转凉″ 提交于 2019-12-12 08:59:53
问题 I am parsing key value pairs (similar to HTTP headers) using boost::spirit::x3 . When comparing the performance to my handwritten parser, boost::spirit::x3 is around 10% slower than that. I am using boost 1.61 and GCC 6.1: $ g++ -std=c++14 -O3 -I/tmp/boost_1_61_0/boost/ main.cpp && ./a.out phrase_parse 1.97432 microseconds parseHeader 1.75742 microseconds How can I improve the performance of the boost::spirit::x3 based parser? #include <iostream> #include <string> #include <map> #include

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

陌路散爱 提交于 2019-12-12 08:58:08
问题 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() {

Is it possible to create a template alias?

走远了吗. 提交于 2019-12-12 08:21:40
问题 Consider the following code: template< template< typename ... > class ... Ts > struct unite { template< typename ... T > struct type : Ts< T ... > ... { }; }; // This does not work as ::type does not name a type, but a template: // template< template< typename ... > class ... Ts > // using unite_t = typename unite< Ts ... >::type; template< typename > struct debug_none {}; template< typename > struct debug_cout {}; template< typename ... > struct raise_demangled {}; template< typename ... >

Why using 0 as default non type template parameter for void* is not allowed

落爺英雄遲暮 提交于 2019-12-12 07:57:19
问题 Why does the following code fail to compile? Even though it is legal to do void* ptr = 0; template <void* ptr = 0> void func(); int main() { func(); return 0; } I ask because I found that a very trusted source did something similar and it failed to compile on my machine NOTE Should have posted the compiler error along with my question so here it is so_test.cpp:1:23: error: null non-type template argument must be cast to template parameter type 'void *' template <void* ptr = 0> ^ static_cast

Simulating the range-based for loop's begin/end behavior

人走茶凉 提交于 2019-12-12 07:45:07
问题 Consider the specification of the range-based for loop's begin-expr and end-expr (N4140 [stmt.ranged]/p1). Given a range __range of type _RangeT , begin-expr and end-expr are determined as follows: if _RangeT is an array type, begin-expr and end-expr are __range and __range + __bound , respectively, where __bound is the array bound. If _RangeT is an array of unknown size or an array of incomplete type, the program is ill-formed; if _RangeT is a class type, the unqualified-id s begin and end

Given int **p1 and const int**p2 is p1 == p2 well formed?

一笑奈何 提交于 2019-12-12 07:41:17
问题 Given the following function: void g(int **p1, const int**p2) { if (p1 == p2) { } } clang ( back to version 3.0 ) produces this warning ( see it live ): warning: comparison of distinct pointer types ('int **' and 'const int **') uses non-standard composite pointer type 'const int *const *' [-Wcompare-distinct-pointer-types] if (p1 == p2) { } ~~ ^ ~~ Using -pedantic-errors flags turns it into an error. Neither gcc ( back to 4.3.6 ) nor Visual Studio ( 2013 ) produce a warning, according to the