c++14

Distinguish between types using SFINAE and void_t

独自空忆成欢 提交于 2019-12-10 23:16:04
问题 I faced some situation where I have to write two functions, one of them should be invoked with primitive types and std::string . The other one should be called with other types. So far I ended with working solution: template <typename...> struct Void_t_helper { using type = void; }; template <typename... Ts> using Void_t = typename Void_t_helper<Ts...>::type; template <typename T, typename = void> struct Is_string : std::false_type {}; template <typename T> struct Is_string<T, Void_t<decltype

Extending namespace std with backported types and templates from newer C++ standards

风流意气都作罢 提交于 2019-12-10 23:10:14
问题 Currently we work on a library that is allowed to use C++11/14 features and types when possible but must fallback to a C++03 implementation otherwise. Users and developers of this library are encouraged to use types and templates from namespace std . Problems arise when using types from namespace std introduced by newer versions of the C++ Standard which are not shipped with the compiler used. One suggested option is to provide 'backports' for certain types that do not rely on new language

Get input/output type of callable

廉价感情. 提交于 2019-12-10 23:08:31
问题 I have the following problem: template<typename Func, typename T_in = /*input type of Func */, typename T_out = /*output type of Func */> std::vector<T_out> foo( Func f, const std::vector<T_in>& input) { std::vector<T_out> res( input.size() ); for( size_t i = 0 ; i < input.size() ; ++i ) res[ i ] = f( input[ i ] ); return res; } int main() { // example for f(x) = x*x std::vector<float> input = { /* ... */ }; auto res = foo( [](float in){ return in*in; }, input ); return 0; } As you can see

Will template constraints be available for variable templates?

戏子无情 提交于 2019-12-10 22:27:31
问题 In the latest template constraints paper a new toolset to constrain template arguments is presented. Also, in C++14 variable templates are provided. Variable templates allow the definition of type parameterized constants among other things. There is no mention of how these feature could interact. Using the canonical example of pi we could have this: template<Integral T> constexpr double pi(3.141592653589793238); template<Floating_point T> constexpr T pi(3.1415926535897932384626433832795029L);

C++11 constexpr causes compiler's internal error (C1001)

别等时光非礼了梦想. 提交于 2019-12-10 21:01:55
问题 I am using Visual Studio 2015 Update 3. I get a fatal error: (code C1001) : An internal error has occurred in the compiler. Here is the code : template<typename T> constexpr T epsilon = std::numeric_limits<T>::epsilon(); I read it was fixed in Visual Studio Update 2. Can someone explain me why I am getting this error? Thanks in advance. 回答1: Any internal error (ICE) is a compiler bug. You get it because you have happened to trigger that bug. For this compiler you can report it at Microsoft

Call function for each tuple element on one object without recursion

时光毁灭记忆、已成空白 提交于 2019-12-10 20:27:18
问题 I have an object of class A that may be called with different types and returns changed self on each call. For purpose of this question A will do struct A { A call(const int&) { } A call(const string& s) { } //// } a; So I have a tuple of unknown types: std::tuple<Types...> t; and I want to call a with each tuple element, so I want to get something like: b = a; b = b.call(get<0>(t)); b = b.call(get<1>(t)); b = b.call(get<2>(t)); //... or b = a.call(get<0>(t)).call(get<1>(t)).call(get<2>(t)...

Select constructor through SFINAE in template arguments

主宰稳场 提交于 2019-12-10 20:26:30
问题 I'm trying to select a constructor through SFINAE as following: template<typename T> class MyClass { public: template<typename C, typename = std::enable_if_t<std::is_class<C>::value>> MyClass(C) { } template<typename C, typename = std::enable_if_t<std::is_pointer<C>::value>> MyClass(C) { } }; but the compiler complains with following error: error C2535: 'MyClass::MyClass(C)': member function already defined or declared without even instantiating the constructor. I worked out a working but

How to debug C++ code in Visual studio code

為{幸葍}努か 提交于 2019-12-10 19:55:12
问题 Anyone using Visual studio code for programming in C++? Please tell me how can i manage to do the debugging of my code in visual studio code when I'm compiling it using g++ compiler. 回答1: C++ Debugging requires a couple of steps to configure VSCode for it. Once done then C++ code can be easily debugged with F5. I wrote a post which guides how to run and debug C/C++ files in VSCode. https://medium.com/@jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6 回答2: To debug

Arity of aggregate in logarithmic time

心不动则不痛 提交于 2019-12-10 19:46:18
问题 How to define arity of an aggregate in logarithmic (at least base two) compilation time (strictly speaking, in logarithmic number of instantiations)? What I can do currently is to achieve desired in a linear time: #include <type_traits> #include <utility> struct filler { template< typename type > operator type (); }; template< typename A, typename index_sequence = std::index_sequence<>, typename = void > struct aggregate_arity : index_sequence { }; template< typename A, std::size_t ...indices

How to write function with parameter which type is deduced with 'auto' word?

微笑、不失礼 提交于 2019-12-10 19:44:48
问题 I am searching a clean c++11 (up to c++17) way to write a function that simply writes fps to the output stream with given 'start' and 'stop' times (e.g. given an interval times). So I have this code, for example: #include <iostream> int main(int argc, char** argv) { typedef std::chrono::high_resolution_clock time_t; while (1) { auto start = time_t::now(); // here is the call of function that do something // in this example will be printing std::cout << "Hello world!" auto stop = time_t::now()