template-deduction

Perfect forwarding with class template argument deduction

孤街浪徒 提交于 2019-12-04 12:26:22
问题 I would like to understand how deductions guides work with universal references and std::forward , in particular to create perfectly forwarding wrappers. The code below provides a code to experiment with a functor wrapper in two cases: one with an implicit deduction guide and one with an explicit deduction guide. I have put a lot of && and std::forward in comments, because I do not know where they are needed to achieve perfect forwarding. I would like to know where to put them, and where they

Is template-name<TT> a deduced context?

安稳与你 提交于 2019-12-04 04:39:57
[temp.deduct.type] paragraph 8 lists all deduced contexts, but it seems not to include template-name <TT> where template-name refers to a class template and TT refers to a template template argument. Is this a deduced context? If it is, why? If not, consider the following code: template<template<typename> class U, template<typename> class V> struct foo {}; template<template<typename> class U> struct foo<U, U> {}; int main() {} This code compiles under Clang 7.0.0 and GCC 8.0.1 , which means the compilers consider the partial specialization is more specialized than the primary template, which

Is it possible to deduce whether type is incomplete without compilation failure?

*爱你&永不变心* 提交于 2019-12-04 03:37:15
问题 I want to achieve behavior like sizeof(complete_type) will return real sizeof, and sizeof(incomplete_type) - will be just 0 I need this to provide extended run time type information for IPC(inter-process) communication with the description structure per type: struct my_type_info { bool is_pointer; size_t size; //for double* will be 4 on i386. that is sizeof(double*) size_t base_size; //for double* will be 8. that is sizeof(double) }; The problem appears when into my system goes something like

Template default argument loses its reference type

本小妞迷上赌 提交于 2019-12-03 14:22:44
问题 Consider #include <iostream> #include <type_traits> template <class T, class ARG_T = T&> T foo(ARG_T v){ return std::is_reference<decltype(v)>::value; } int main() { int a = 1; std::cout << foo<int>(a) << '\n'; std::cout << foo<int, int&>(a) << '\n'; } I'd expect the output to be 1 in both cases. But in the first case it's 0: consistent with the default being class ARG_T = T rather than class ARG_T = T& . What am I missing? 回答1: For foo<int>(a) , ARG_T is being deduced from a , and is not

Template Argument Deduction Failure and Function Parameters/Arguments Mismatch

心不动则不痛 提交于 2019-12-03 12:25:35
Consider the following program: template <class T> struct A { using X = typename T::X; }; template <class T, typename A<T>::X* = nullptr> void f(T, int); void f(...); template <class T> void g(T, int, typename A<T>::X* = nullptr); // # void g(...); int main() { // f(0, nullptr); // error g(0, nullptr); // ok } g(0, nullptr) compiles while f(0, nullptr) does not (tested under GCC trunk and Clang trunk on Godbolt ). It seems that during the template argument deduction process of # , the compiler does not instantiate A<int> when it finds the argument nullptr does not match the parameter int .

Class template argument deduction and default template parameters

Deadly 提交于 2019-12-03 10:40:05
The following stripped down code doesn't work with the latest clang++5 but is accepted by g++7: template<typename Wrapped, typename U> struct wrapper; template<typename Wrapped, typename U=int> struct wrapper { wrapper() = default; // Automatic deduction guide constexpr explicit wrapper(Wrapped) noexcept {} }; int main() { struct {} dummy; constexpr auto wrapped = wrapper(dummy); } It fails with the following error messages: <source>:18:30: error: no viable constructor or deduction guide for deduction of template arguments of 'wrapper' constexpr auto wrapped = wrapper(dummy); ^ <source>:12:24:

Perfect forwarding with class template argument deduction

北城以北 提交于 2019-12-03 07:57:12
I would like to understand how deductions guides work with universal references and std::forward , in particular to create perfectly forwarding wrappers. The code below provides a code to experiment with a functor wrapper in two cases: one with an implicit deduction guide and one with an explicit deduction guide. I have put a lot of && and std::forward in comments, because I do not know where they are needed to achieve perfect forwarding. I would like to know where to put them, and where they are not needed. // Case with not conversion constructor template <class F> struct functor1 { explicit

Template default argument loses its reference type

依然范特西╮ 提交于 2019-12-03 04:12:06
Consider #include <iostream> #include <type_traits> template <class T, class ARG_T = T&> T foo(ARG_T v){ return std::is_reference<decltype(v)>::value; } int main() { int a = 1; std::cout << foo<int>(a) << '\n'; std::cout << foo<int, int&>(a) << '\n'; } I'd expect the output to be 1 in both cases. But in the first case it's 0: consistent with the default being class ARG_T = T rather than class ARG_T = T& . What am I missing? For foo<int>(a) , ARG_T is being deduced from a , and is not taken from the default template argument. Since it's a by value function parameter, and a is an expression of

Partial template function specification in C++ works, but why?

非 Y 不嫁゛ 提交于 2019-12-03 02:08:13
I'm trying to find out whether partial specification of templated functions is part of the C++ standard, or whether this is something compiler specific. By partial specification, I mean specifying only the types the compiler can't deduce. So if I have a template function 'f' that takes 3 types, and one is used in a parameter and can be deduced, I might call 'f' with the form f<type, type>(parameter) Here's an example: #include <iostream> #include <tuple> #include <string> template<class A, class B, class C> std::tuple<A, B> test(C c) { // do something based on c, return tuple with types A and

Make a function accepting an optional to accept a non-optional?

荒凉一梦 提交于 2019-12-02 17:24:37
I'm trying to write syntactic sugar, in a monad-style, over std::optional . Please consider: template<class T> void f(std::optional<T>) {} As is, this function cannot be called with a non-optional T 1 (e.g. an int ), even though there exists a conversion from T to std::optional<T> 2 . Is there a way to make f accept an std::optional<T> or a T (converted to an optional at the caller site), without defining an overload 3 ? 1) f(0) : error: no matching function for call to 'f(int)' and note: template argument deduction/substitution failed , ( demo ). 2) Because template argument deduction doesn't