Duplicate code using c++11

Something like that will do nicely: template<bool bonus = false> void MyFunction() { foo(); bar(); if (bonus) { doBonusStuff(); } foobar(); } Call it via: MyFunction<true>(); MyFunction<false>(); MyFunction(); // Call myFunction with the false template by default The “ugly” template can be all avoided by adding some nice wrappers to the functions: void MyFunctionAlone() { … Read more

There are no arguments that depend on a template parameter

For builtin types, argument dependent lookup (ADL) is not performed, therefore, an ignore symbol must be “imported” into the current namespace. You can, for example, do this; from most preferred to least preferred (i.e. to most intrusive and name polluting): foobar::ignore (…) using foobar::ignore; ignore(…); using namespace foobar; ignore(…); The error message comes up like … Read more

How do you print a C++11 time_point?

(In this post I will omit std::chrono:: qualifications for clarity. I trust you know where they go.) The reason your code example fails to compile is that there is a mismatch between the return type of system_clock::now() and the type of variable you are trying to assign this to (time_point<system_clock, nanoseconds>). The documented return value … Read more

How to detect whether there is a specific member variable in class?

Here is a solution simpler than Johannes Schaub – litb’s one. It requires C++11. #include <type_traits> template <typename T, typename = int> struct HasX : std::false_type { }; template <typename T> struct HasX <T, decltype((void) T::x, 0)> : std::true_type { }; Update: A quick example and the explanation on how this works. For these types: … Read more

Two phase lookup – explanation needed

Templates are compiled (at least) twice: Without Instantiation the template code itself is checked for syntax. Eg: Any syntax errors such as ; etc. At the time of instantiation(when the exact type is known), the template code is checked again to ensure all calls are valid for that particular type. Eg: The template might in … Read more

How to typedef a template class? [duplicate]

Yes. It is called an “alias template,” and it’s a new feature in C++11. template<typename T> using MyVector = std::vector<T, MyCustomAllocator<T>>; Usage would then be exactly as you expect: MyVector<int> x; // same as: std::vector<int, MyCustomAllocator<int>> GCC has supported it since 4.7, Clang has it since 3.0, and MSVC has it in 2013 SP4.

What is a nondeduced context?

Deduction refers to the process of determining the type of a template parameter from a given argument. It applies to function templates, auto, and a few other cases (e.g. partial specialization). For example, consider: template <typename T> void f(std::vector<T>); Now if you say f(x), where you declared std::vector<int> x;, then T is deduced as int, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)