template-deduction

Can't deduce template type

爷,独闯天下 提交于 2019-11-30 03:03:35
问题 I'm trying to pass an iterator as a template parameter to a template method, but the compiler complains that: error C2783: 'void Test::Assert(std::vector<T>::const_iterator)': could not deduce template argument for 'T' The code that produces the error is: #include "stdafx.h" #include <iostream> #include <vector> class Test { public: template <typename T> void Assert(typename std::vector<T>::const_iterator it) { std::cout << *it << std::endl; } }; int _tmain(int argc, _TCHAR* argv[]) { Test

template argument deduction with strongly-typed enumerations

天涯浪子 提交于 2019-11-30 00:07:22
If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so: enum { Cat, Dog, Horse }; template <int Val, typename T> bool magic(T &t) { return magical_traits<Val>::invoke(t); } and call it as: magic<Cat>(t) as far as I can see, if I have a strongly-typed enumeration and don't want to hard-code the enumeration type, I end up with: enum class Animal { Cat, Dog, Horse }; template <typename EnumClass, EnumClass EnumVal, typename T> bool magic(T &t) { return magical_traits<EnumVal>::invoke(t); } and now I have to write: magic<Animal, Animal::Cat>

Can I generate a function without providing arguments?

廉价感情. 提交于 2019-11-29 16:23:10
So c++17 has std::function Deduction Guides so given: int foo(); I can do: std::function bar(foo); But I'm stuck on a c++14 compiler. There I have to do something more like: function<int()> bar(foo) . I was wondering if there was a way to create a std::function without passing the function pointer and explicitly providing the function signature? So for example make_pair will deduce the type of it's return from it's arguments. I was wondering if I could write something similar for function s even using c++14 , like: auto bar = make_function(foo); Is this doable? Note: My real case is that foo

C++17 template deduction guide not used for empty parameter set?

我们两清 提交于 2019-11-29 11:55:00
Consider the following reduced example which can also be viewed at https://godbolt.org/g/Et56cm : #include <utility> template <class T> struct success { T value; constexpr success(T &&v) : value(std::move(v)) { } constexpr success(const T &v) : value(v) { } }; template <> struct success<void> { }; template <class T> success(T /*unused*/)->success<T>; success()->success<void>; int main(void) { auto a = success{5}; // works auto b = success{}; // works auto c = success{"hello"}; // works auto d = success(5); // works auto e = success(); // FAILS! auto f = success("hello"); // works static_assert

Is there a way to deduce the value of a function pointer template parameter?

﹥>﹥吖頭↗ 提交于 2019-11-29 10:47:48
C++ allows non-type template parameters to be of pointer, including function pointer, type. I recently asked a question about what this is useful for, and this is a follow up to one of the answers . Is it posible to deduce the value of a function pointer template parameter, from a function argument that is the function pointer in question? For example: using VoidFunction = void(*)(); template <VoidFunction F> void templ(VoidFunction); ... void func(); // a VoidFunction ... templ<func>(func); // works, but I have to specify the template parameter explicitly templ(func); // <-- I would like to

template argument deduction with strongly-typed enumerations

混江龙づ霸主 提交于 2019-11-28 21:09:32
问题 If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so: enum { Cat, Dog, Horse }; template <int Val, typename T> bool magic(T &t) { return magical_traits<Val>::invoke(t); } and call it as: magic<Cat>(t) as far as I can see, if I have a strongly-typed enumeration and don't want to hard-code the enumeration type, I end up with: enum class Animal { Cat, Dog, Horse }; template <typename EnumClass, EnumClass EnumVal, typename T> bool magic(T

Constructing std::function argument from lambda

為{幸葍}努か 提交于 2019-11-28 13:55:37
I have the following templated function (C++ latest standard is enabled in the compiler - but maybe 17 would be enough). #include <functional> template<typename TReturn, typename ...TArgs> void MyFunction(const std::function<TReturn(TArgs...)>& callback); int main() { MyFunction(std::function([](int){})); MyFunction([](int){}); } The first call compiles, when I explicitly convert it to std::function, but the second case does not. In the first case the template deduction is done automatically, the compiler only knows that it shall convert it to some std::function and able to deduce the

Deduction guide and variadic templates

我与影子孤独终老i 提交于 2019-11-28 13:27:23
Consider the following code: #include <tuple> #include <iostream> template <class T> struct custom_wrapper { template <class Arg> custom_wrapper(Arg arg): data(arg) {} T data; }; template <class Arg> custom_wrapper(Arg arg) -> custom_wrapper<Arg>; template <class... T> struct custom_tuple { template <class... Args> custom_tuple(Args... args): data(args...) {} std::tuple<T...> data; }; template <class... Args> custom_tuple(Args... args) -> custom_tuple<Args...>; int main(int argc, char* argv[]) { custom_wrapper<int> w1(42); // OK custom_wrapper w2(42); // OK custom_tuple<int> t1(42); // OK

C++17 template deduction guide not used for empty parameter set?

有些话、适合烂在心里 提交于 2019-11-28 05:16:41
问题 Consider the following reduced example which can also be viewed at https://godbolt.org/g/Et56cm: #include <utility> template <class T> struct success { T value; constexpr success(T &&v) : value(std::move(v)) { } constexpr success(const T &v) : value(v) { } }; template <> struct success<void> { }; template <class T> success(T /*unused*/)->success<T>; success()->success<void>; int main(void) { auto a = success{5}; // works auto b = success{}; // works auto c = success{"hello"}; // works auto d

Is there a way to deduce the value of a function pointer template parameter?

元气小坏坏 提交于 2019-11-28 03:58:35
问题 C++ allows non-type template parameters to be of pointer, including function pointer, type. I recently asked a question about what this is useful for, and this is a follow up to one of the answers. Is it posible to deduce the value of a function pointer template parameter, from a function argument that is the function pointer in question? For example: using VoidFunction = void(*)(); template <VoidFunction F> void templ(VoidFunction); ... void func(); // a VoidFunction ... templ<func>(func); /