How to check for the type of a template parameter?

Use is_same: #include <type_traits> template <typename T> void foo() { if (std::is_same<T, animal>::value) { /* … */ } // optimizable… } Usually, that’s a totally unworkable design, though, and you really want to specialize: template <typename T> void foo() { /* generic implementation */ } template <> void foo<animal>() { /* specific for T = … Read more

Inheriting from a template class in c++

For understanding templates, it’s of huge advantage to get the terminology straight because the way you speak about them determines the way to think about them. Specifically, Area is not a template class, but a class template. That is, it is a template from which classes can be generated. Area<int> is such a class (it’s … Read more

C++ templates Turing-complete?

I’ve done a turing machine in C++11. Features that C++11 adds are not significant for the turing machine indeed. It just provides for arbitrary length rule lists using variadic templates, instead of using perverse macro metaprogramming :). The names for the conditions are used to output a diagram on stdout. i’ve removed that code to … Read more

How to easily map c++ enums to strings

If you want the enum names themselves as strings, see this post. Otherwise, a std::map<MyEnum, char const*> will work nicely. (No point in copying your string literals to std::strings in the map) For extra syntactic sugar, here’s how to write a map_init class. The goal is to allow std::map<MyEnum, const char*> MyMap; map_init(MyMap) (eValue1, “A”) … Read more

How can I output the value of an enum class in C++11

Unlike an unscoped enumeration, a scoped enumeration is not implicitly convertible to its integer value. You need to explicitly convert it to an integer using a cast: std::cout << static_cast<std::underlying_type<A>::type>(a) << std::endl; You may want to encapsulate the logic into a function template: template <typename Enumeration> auto as_integer(Enumeration const value) -> typename std::underlying_type<Enumeration>::type { return … Read more

What does template mean?

Yes, it is a non-type parameter. You can have several kinds of template parameters Type Parameters. Types Templates (only classes and alias templates, no functions or variable templates) Non-type Parameters Pointers References Integral constant expressions What you have there is of the last kind. It’s a compile time constant (so-called constant expression) and is of … Read more

C++ template constructor

There is no way to explicitly specify the template arguments when calling a constructor template, so they have to be deduced through argument deduction. This is because if you say: Foo<int> f = Foo<int>(); The <int> is the template argument list for the type Foo, not for its constructor. There’s nowhere for the constructor template’s … Read more

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