template-meta-programming

Restricting templates to only certain classes?

折月煮酒 提交于 2019-12-21 13:09:10
问题 In Java you can restrict generics so that the parameter type is only a subclass of a particular class. This allows the generics to know the available functions on the type. I haven't seen this in C++ with templates. So is there a way to restrict the template type and if not, how does the intellisense know which methods are available for <typename T> and whether your passed-in type will work for the templated function? 回答1: As of C++11, there is no way to constrain template type arguments. You

Alternatives for compile-time floating-point initialization

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 06:57:48
问题 I'm currently working on a template-meta-programming based implementation of floating-point arithmetic. The template which represent compile-time float values is as follows: template<bool S , std::int16_t E , std::uint64_t M> struct number{}; Since initializing such values using hardcoded mantissas, exponents, etc, is a cumbersome and bug-prone process I have written a template for converting decimal values to floating-point ones: template<std::int64_t INT , std::uint64_t DECS> struct decimal

detecting protected constructors of (possibly abstract) base class

99封情书 提交于 2019-12-21 03:29:17
问题 I am experimenting with the new features of C++11. In my setup I would really love to use inheriting constructors, but unfortunately no compiler implements those yet. Therefore I am trying to simulate the same behaviour. I can write something like this: template <class T> class Wrapper : public T { public: template <typename... As> Wrapper(As && ... as) : T { std::forward<As>(as)... } { } // ... nice additions to T ... }; This works... most of the time. Sometimes the code using the Wrapper

Compile-time equivalent to std::accumulate()

青春壹個敷衍的年華 提交于 2019-12-21 01:09:08
问题 I tried to code a basic, compile-time version of std::accumulate() by defining a class template that would recursively iterate through a given range and would add the elements at each iteration. When compiling a test program using gcc 4.8.4 on Ubuntu 14.04 , I get the following error: compile-time-accumulate.cpp: In function ‘int main()’: compile-time-accumulate.cpp:44:40: error: call to non-constexpr function ‘std::vector<_Tp, _Alloc>::const_iterator std::vector<_Tp, _Alloc>::cbegin() const

Russell's paradox in C++ templates [duplicate]

落花浮王杯 提交于 2019-12-20 17:28:03
问题 This question already has an answer here : Fallback variadic constructor - why does this work? (1 answer) Closed 2 years ago . Consider this program: #include <iostream> #include <type_traits> using namespace std; struct russell { template <typename barber, typename = typename enable_if<!is_convertible<barber, russell>::value>::type> russell(barber) {} }; russell verify1() { return 42L; } russell verify2() { return 42; } int main () { verify1(); verify2(); cout << is_convertible<long, russell

Russell's paradox in C++ templates [duplicate]

心不动则不痛 提交于 2019-12-20 17:27:08
问题 This question already has an answer here : Fallback variadic constructor - why does this work? (1 answer) Closed 2 years ago . Consider this program: #include <iostream> #include <type_traits> using namespace std; struct russell { template <typename barber, typename = typename enable_if<!is_convertible<barber, russell>::value>::type> russell(barber) {} }; russell verify1() { return 42L; } russell verify2() { return 42; } int main () { verify1(); verify2(); cout << is_convertible<long, russell

How to build a compile-time key/value store?

痞子三分冷 提交于 2019-12-20 09:57:54
问题 I have a problem where I need to map an integer at compile time to another integer. Basically, I need the compile-time equivalent of std::map<int,int> . If a key is not found in the map, I'd like to return a default value. The interface I'd like to use: template<unsigned int default_value, unsigned int key0, unsigned int value0, unsigned int key1, unsigned int value1, ...> struct static_map { ... }; template<unsigned int key, typename StaticMap> struct lookup { static unsigned int value = ...

Variadic templates and switch statement?

那年仲夏 提交于 2019-12-20 09:16:37
问题 I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with two arguments): template <typename T1, typename T2> bool func(int& counter, T1 x1, T2 x2) { switch (counter) { case 0: if (func2<T1>(x1)) { counter++; return true; } else { return false; } case 1: if (func2<T2>(x2)) { counter++; return true; } else { return false; } default: return true; } } I want to write this function

Accessing base member data error when derived class is templated

纵然是瞬间 提交于 2019-12-20 04:28:13
问题 I have the following problem with the curiously recurring template, with a problem when I try to access the data member of CRTP base class. template<typename T> struct Base { int protectedData=10; }; struct Derived : public Base<Derived> { public: void method() { std::cout<<protectedData<<std::endl; }; }; int main () { Derived a; a.method(); } The above code compiles and runs fine and I can get "10" printed, but if I have the derived class templated, like: template<typename T> struct Base {

Template argument deduction when the function returns a type composed from the template type and another

假如想象 提交于 2019-12-20 03:08:30
问题 The title is rather hard to formulate in word, but here is what I'm trying to achieve in non-compileable code: template<template <typename> class Container> Container<int> foo() { return Container<int>{1,2,3}; } int main() { auto bar = foo<std::vector>(); return 0; } Basically I want a template function that can "compose" its return type from a type that is passed to it and a previously known type (in this case int ). In this case I want a function that returns an arbitrary data type inside a