template-deduction

Why can template instances not be deduced in `std::reference_wrapper`s?

試著忘記壹切 提交于 2019-11-27 23:33:53
Suppose I have some object of type T , and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper<int> p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q) , because the reference wrapper has a conversion to its wrapped type. All is happy, and I can process a collection of reference wrappers just like they were the original objects. (As the question linked below shows, this can be a useful way to produce an alternate view of an existing collection, which can be rearranged at will without incurring the cost of a full copy, as well as maintaining

Deduce template argument from std::function call signature

若如初见. 提交于 2019-11-26 23:10:56
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun(); } Why isn't it possible for the compiler to deduce ReturnT from the passed call signature? bool bar() { /* ... */ } foo<bool>(bar); // works foo(bar); // error: no matching function call std::function<bool()> bar; foo(bar); // works just fine C++ can't deduce the return type from your function bar because it would have to know the type before it could find all the constructors that take your function pointer. For example, who's to say that std::function<std::string()>

Why can template instances not be deduced in `std::reference_wrapper`s?

三世轮回 提交于 2019-11-26 21:12:35
问题 Suppose I have some object of type T , and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper<int> p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q) , because the reference wrapper has a conversion to its wrapped type. All is happy, and I can process a collection of reference wrappers just like they were the original objects. (As the question linked below shows, this can be a useful way to produce an alternate view of an existing

Partial ordering with function template having undeduced context

无人久伴 提交于 2019-11-26 19:52:08
问题 While reading another question, i came to a problem with partial ordering, which i cut down to the following test-case template<typename T> struct Const { typedef void type; }; template<typename T> void f(T, typename Const<T>::type*) { cout << "Const"; } // T1 template<typename T> void f(T, void*) { cout << "void*"; } // T2 int main() { // GCC chokes on f(0, 0) (not being able to match against T1) void *p = 0; f(0, p); } For both function templates, the function type of the specialization

What is the partial ordering procedure in template deduction

南楼画角 提交于 2019-11-26 17:28:43
Reading the C++11 standard I can't fully understand the meaning of the following statement. Example are very welcome. Two sets of types are used to determine the partial ordering. For each of the templates involved there is the original function type and the transformed function type. [Note: The creation of the transformed type is described in 14.5.6.2. — end note ] The deduction process uses the transformed type as the argument template and the original type of the other template as the parameter template. This process is done twice for each type involved in the partial ordering comparison:

Why do auto and template type deduction differ for braced initializers?

别等时光非礼了梦想. 提交于 2019-11-26 16:50:22
问题 I understand that, given a braced initializer, auto will deduce a type of std::initializer_list , while template type deduction will fail: auto var = { 1, 2, 3 }; // type deduced as std::initializer_list<int> template<class T> void f(T parameter); f({ 1, 2, 3 }); // doesn't compile; type deduction fails I even know where this is specified in the C++11 standard: 14.8.2.5/5 bullet 5: [It's a non-deduced context if the program has] A function parameter for which the associated argument is an

Workaround for template argument deduction in non-deduced context

戏子无情 提交于 2019-11-26 09:02:30
问题 Consider the following code: #include <iostream> template<class T> struct outer { struct inner {}; }; template<class T> std::ostream& operator<<(std::ostream & stream, typename outer<T>::inner const& value) { std::cout << \"An outer::inner!\"; return stream; } int main() { outer<float>::inner foo; std::cout << foo << std::endl; // does not compile } This does not compile, because typename outer<T>::inner is a nondeduced context (as explained here), meaning the template-argument-type cannot be

Variadic deduction guide not taken by g++, taken by clang++ - who is correct?

ε祈祈猫儿з 提交于 2019-11-26 07:47:54
问题 Consider the following code: template <typename... Types> struct list { template <typename... Args> list(Args...) { static_assert(sizeof...(Types) > 0); } }; template <typename... Args> list(Args...) -> list<Args...>; int main() { list l{0, 0.1, \'a\'}; } I would expect decltype(l) to be list<int, double, char> . Unfortunately, g++ 7.2 and g++ trunk fail the static assertion. clang++ 5.0.0 and clang++ trunk compile and work as expected. godbolt.org conformance view Is this a g++ bug? Or Is

What is the partial ordering procedure in template deduction

寵の児 提交于 2019-11-26 05:26:34
问题 Reading the C++11 standard I can\'t fully understand the meaning of the following statement. Example are very welcome. Two sets of types are used to determine the partial ordering. For each of the templates involved there is the original function type and the transformed function type. [Note: The creation of the transformed type is described in 14.5.6.2. — end note ] The deduction process uses the transformed type as the argument template and the original type of the other template as the

What is a nondeduced context?

风格不统一 提交于 2019-11-25 22:27:14
问题 I\'ve stumbled over \" Why is the template argument deduction not working here? \" recently and the answers can be summed up to \"It\'s a nondeduced context\". Specifically, the first one says it\'s such a thing and then redirects to the standard for \"details\", while the second one quotes the standard, which is cryptic to say the least. Can someone please explain to mere mortals, like myself, what a nondeduced context is, when does it occur, and why does it occur? 回答1: Deduction refers to