template-meta-programming

meaning of factorial<T - 1> in template definition

一笑奈何 提交于 2021-01-29 04:56:05
问题 I have difficulties to understand how the following template definition and template specialization definition work? To me, factorial<34> or factorial<T-1> look strange! For example: factorial<T - 1>::value means what? #include <iostream> template<int T> struct factorial { enum { value = factorial<T - 1>::value * T }; }; template<> struct factorial<1> { enum { value = 1 }; }; int main() { std::cout << factorial<34>::value << std::endl; } g++ -o testSTL01 testSTL01.cpp -Wall testSTL01.cpp: In

Combining n vectors into one vector of n-tuples

南笙酒味 提交于 2021-01-27 11:44:35
问题 I'm thinking about a function with signature template<typename ...Ts> std::vector<std::tuple<Ts...>> join_vectors(std::vector<Ts>&&...) { //... }; but probably a more general one accepting any iterable instead of just std::vector would be good. Probably it would have a signature like this? template<template<typename> typename C, typename ...Ts> C<std::tuple<Ts...>> join_vectors(C<Ts>&&...) { // ... }; However, I'm not at this level yet in C++ (despite doing the same in Haskell would be

Adding a const qualifier to a member function

偶尔善良 提交于 2021-01-27 05:39:46
问题 I am currently writing an interface class that should provide access to to internal elements of a complex structure as const or non-const references. The idea is that some modules are granted const access and some modules are granted full access. I have used the 'type_traits' 'std::add_const' to conditionally qualify the return type of the internal member functions, unfortunately I cannot think of a way of conditionally qualifiying the member functions as const or non-const. Is this even

Adding a const qualifier to a member function

白昼怎懂夜的黑 提交于 2021-01-27 05:39:40
问题 I am currently writing an interface class that should provide access to to internal elements of a complex structure as const or non-const references. The idea is that some modules are granted const access and some modules are granted full access. I have used the 'type_traits' 'std::add_const' to conditionally qualify the return type of the internal member functions, unfortunately I cannot think of a way of conditionally qualifiying the member functions as const or non-const. Is this even

Why isn't this class specialization using a concept accepted?

▼魔方 西西 提交于 2020-11-29 08:58:09
问题 The following code attempts to partially specialize a class using a concept and add a method to the specialization, but it is rejected by clang 11.0.0: #include <concepts> template <typename T> // note: previous template declaration is here struct S {}; template <std::integral T> struct S<T> { void f(); }; template <std::integral T> // error: type constraint differs in template redeclaration void S<T>::f() { } clang gives the error message: <source>:14:16: error: type constraint differs in