In std::exchange, why is the second template parameter defaulted?

The std::exchange was proposed in N3511 without default template argument, and later N3608 with a default template argument. Note that in N3608 the following reasoning was provided: Giving the second template argument a default value fixes the following two cases: DefaultConstructible x = …; if (exchange(x, {})) { … } int (*fp)(int); int f(int); double … Read more

How to write a trait which checks whether a type is iterable

You may create a trait for that: namespace detail { // To allow ADL with custom begin/end using std::begin; using std::end; template <typename T> auto is_iterable_impl(int) -> decltype ( begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator != void(), // Handle evil operator , ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++ void(*begin(std::declval<T&>())), // operator* std::true_type{}); template <typename T> std::false_type … Read more

C++ Static member initialization (template fun inside)

This was discussed on usenet some time ago, while i was trying to answer another question on stackoverflow: Point of Instantiation of Static Data Members. I think it’s worth reducing the test-case, and considering each scenario in isolation, so let’s look at it more general first: struct C { C(int n) { printf(“%d\n”, n); } … Read more

How to instantiate only part of a function template if a condition is true

Type traits: #include <iostream> #include <type_traits> // C++0x //#include <tr1/type_traits> // C++03, use std::tr1 template<typename T> void printType(T param) { if(std::is_same<T,char*>::value) std::cout << “char*” << endl; else if(std::is_same<T,int>::value) std::cout << “int” << endl; else std::cout << “???” << endl; } Or even better yet, just overload the function: template<class T> void printType(T partam){ std::cout << … Read more

Why can’t T be deduced from Template::Type? [duplicate]

That is non-deducible context. That is why the template argument cannot be deduced by the compiler. Just imagine if you might have specialized TMap as follows: template <> struct TMap<SomeType> { typedef std::map <double, double> Type; }; How would the compiler deduce the type SomeType, given that TMap<SomeType>::Type is std::map<double, double>? It cannot. It’s not … Read more

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