What is a non-deduced 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

Why can you implement std::is_function in terms of std::is_const and std::is_reference?

Let’s go over the conditions as they appear: If const T isn’t const (const doesn’t really apply to function types since functions aren’t objects), and T isn’t a reference (const doesn’t apply to references either for the same reason), it’s a function type. int (or any other non-function-non-reference type) wouldn’t fit in because is_const<const int>::value … Read more

How do you loop over a parameter pack using a pack expansion?

One of the places where a pack expansion can occur is inside a braced-init-list. You can take advantage of this by putting the expansion inside the initializer list of a dummy array: template<typename… Args> static void foo2(Args &&… args) { int dummy[] = { 0, ( (void) bar(std::forward<Args>(args)), 0) … }; } To explain the … Read more

Why can’t I use std::string as a non-type template parameter?

The reason you can’t do this is because non-constant expressions can’t be parsed and substituted during compile-time. They could change during runtime, which would require the generation of a new template during runtime, which isn’t possible because templates are a compile-time concept. Here’s what the standard allows for non-type template parameters (14.1 [temp.param] p4): A … Read more

Why can I seemingly define a partial specialization for function templates?

Function partial specialization is not allowed yet as per the standard. In the example, you are actually overloading & not specializing the max<T1,T2> function. Its syntax should have looked somewhat like below, had it been allowed: // Partial specialization is not allowed by the spec, though! template <typename T> inline T const& max<T,T> (T const& … Read more

Why are default template arguments only allowed on class templates?

It makes sense to give default template arguments. For example you could create a sort function: template<typename Iterator, typename Comp = std::less< typename std::iterator_traits<Iterator>::value_type> > void sort(Iterator beg, Iterator end, Comp c = Comp()) { … } C++0x introduces them to C++. See this defect report by Bjarne Stroustrup: Default Template Arguments for Function Templates … Read more

How do you constrain a template to only accept certain types

This typically is unwarranted in C++, as other answers here have noted. In C++ we tend to define generic types based on other constraints other than “inherits from this class”. If you really wanted to do that, it’s quite easy to do in C++11 and <type_traits>: #include <type_traits> template<typename T> class observable_list { static_assert(std::is_base_of<list, T>::value, … Read more

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