template-meta-programming

How do I use std::enable_if with a self-deducing return type?

≡放荡痞女 提交于 2019-12-04 08:28:30
问题 C++14 will have functions whose return type can be deduced based on the return value. auto function(){ return "hello world"; } Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom? For example, let's consider the following two functons: #include <type_traits> #include <iostream> //This function is chosen when an integral type is passed in template<class T > auto function(T t) -> typename std::enable_if<std::is_integral<T>::value>::type { std::cout <<

What are the coolest examples of metaprogramming that you've seen in C++? [closed]

不想你离开。 提交于 2019-12-04 08:07:03
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What are the coolest examples of metaprogramming that you've seen in C++? What are some practical uses of metaprogramming that you've

different class implementations based on template parameter

寵の児 提交于 2019-12-04 05:47:37
I suppose this is trivial for people who know templates... Suppose we want two different implementations of this template class, depending on the value of N: template <int N> class Foo { ... }; For example: template <int N> class Foo { ... // implementation for N <= 10 }; template <int N> class Foo { ... // implementation for N > 10 }; How can we do that in C++11? Use an extra template parameter with a default value to distinguish cases: template <int N, bool b = N <= 10> class Foo; template <int N> class Foo<N, true> { ... // implementation for N <= 10 }; template <int N> class Foo<N, false>

Restricting templates to only certain classes?

妖精的绣舞 提交于 2019-12-04 05:45:08
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? As of C++11, there is no way to constrain template type arguments. You can, however, make use of SFINAE to ensure that a template is only instantiated for particular types. See

Is it possible to invoke a method with all possible K-combinations (with repetition) of arguments passed in a tuple?

99封情书 提交于 2019-12-04 04:20:11
问题 The desired behaviour can be illustrated as follows: void foo(int x, int y) { std::cout << x << " " << y << std::endl; } int main() { all_combinations<2>(foo, std::make_tuple(1, 2)); // K = 2 // to run: // foo(1, 1) // foo(1, 2) // foo(2, 1) // foo(2, 2) } 回答1: The c++14 version could look as follows: #include <tuple> #include <utility> #include <iostream> #include <initializer_list> template <class Foo, class Tuple, size_t... Is, size_t... Is2> int all_combinations_impl(Foo foo, Tuple t, std

How to check whether a type is std::vector::iterator at compile time?

痴心易碎 提交于 2019-12-04 04:02:51
I have a problem where I need to detect whether a given type is an instance of a known nested type such as std::vector::iterator at compile time. I'd like to create the type trait is_std_vector_iterator : #include <type_traits> #include <vector> template<typename T> struct is_std_vector_iterator : std::false_type {}; template<typename T, typename Allocator> struct is_std_vector_iterator<typename std::vector<T,Allocator>::iterator> : std::true_type {}; int main() { return 0; } But I receive the compiler error: $ g++ -std=c++0x test.cpp test.cpp:7: error: template parameters not used in partial

How to check whether T is an aggregate type?

℡╲_俬逩灬. 提交于 2019-12-04 03:23:20
问题 I know about std::is_pod . But it checks more than just aggregate types. Or, is std::is_pod just the best we can do? Basically, I want to write a function template for this: template <typename T> aggregate_wrapper<T> wrap(T&& x); which is only enabled when T is an aggregate type. 回答1: There is no way to synthesize an is_aggregate template. The rules for whether something participates in aggregate initialization cannot be detected by C++14 metaprogramming techniques (they would require

Getting the type of a member

江枫思渺然 提交于 2019-12-04 01:00:08
NOTE : This question was originally asked way back in 2012. Before the decltype specifier was fully implemented by any major compilers. You should not be looking at this code unless you only have access to C++03. All major C++11 compliant compilers now support decltype . Is there an easy way to retrieve the type of a member? In C++03 struct Person { std::string name; int age; double salary; }; int main() { std::vector<Person> people; // get a vector of people. std::vector<GET_TYPE_OF(Person::age)> ages; ages.push_back(people[0].age); ages.push_back(people[10].age); ages.push_back(people[13]

Constexpr if alternative

北城余情 提交于 2019-12-03 23:57:17
问题 I would like to use constexpr if to branch at compile time, but it does not seem to be supported by the latest MSVC compiler. Is there an alternative to the following?: template<typename T> void MyFunc() { if constexpr(MeetsConditions<T>::value) { FunctionA<T>(); } else { FunctionB<T>(); } } In short: Can I simulate constexpr if when it is not supported by the compiler? 回答1: One of pre-C++17 ways is to use partial template specializations, like here: template <template T, bool AorB> struct

Alternatives for compile-time floating-point initialization

牧云@^-^@ 提交于 2019-12-03 22:36:36
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{}; Where the first parameter represents the integral part and the second the fractional digits. I