template-deduction

g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type

六眼飞鱼酱① 提交于 2019-12-10 12:44:33
问题 Another "who's right between g++ and clang++?" question for C++ standard gurus. Given the following code #include <utility> template <std::size_t N, typename = std::make_index_sequence<N>> struct foo; template <std::size_t N, std::size_t ... Is> struct foo<N, std::index_sequence<Is...>> { }; template <std::size_t N> void bar (foo<N> const &) { } int main() { bar(foo<42u>{}); } I see that g++ compile where clang++ gives the following error tmp_003-14,gcc,clang.cpp:32:4: error: no matching

Template non-type parameter deduction

泪湿孤枕 提交于 2019-12-10 05:16:55
问题 Is it possible to deduce template value (not type) for a c++17 function? The function foo: template<int I> int foo() { return (I); } Can be called via: foo<5>(); And will return 5. Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example: template<int I = x> int bar(const int x) { return (I); } This obviously wont work (because for one x is required before its declaration), but might there be some C+

Why forwarding reference does not deduce to rvalue reference in case of rvalue?

拥有回忆 提交于 2019-12-10 04:31:05
问题 I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&& ). Thus,to allow only rvalues, one need to write template<class T, enable_if<not_<is_lvalue_reference<T> >,OtherConds... > = yes> void foo(T&& x) {} and not, template<class T, enable_if<is_rvalue_reference<T>,OtherConds... > = yes> void foo(T&& x) {} My question is , why for forwarding references, rvalues are deduced to be of type T and

How to provide deduction guide for nested template class?

让人想犯罪 __ 提交于 2019-12-09 14:17:41
问题 According to [temp.deduct.guide/3]: (...) A deduction-guide shall be declared in the same scope as the corresponding class template and, for a member class template, with the same access. (...) But below example doesn't seem to compile in both [gcc] and [clang]. #include <string> template <class> struct Foo { template <class T> struct Bar { Bar(T) { } }; Bar(char const*) -> Bar<std::string>; }; int main() { Foo<int>::Bar bar("abc"); static_cast<void>(bar); } What is the correct syntax of

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

故事扮演 提交于 2019-12-08 22:49:16
问题 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:

Is it guaranteed that template template parameter invoke user provided deduction guides

走远了吗. 提交于 2019-12-08 19:16:20
问题 Consider an example: #include <type_traits> #include <string> template <template <class> class TT> //#1 struct Foo { static void foo() { static_assert(std::is_same_v<decltype(TT("abc")), TT<std::string>>); } }; template <class T> struct Bar { Bar(T) {} }; template <class T> Bar(T) -> Bar<std::string>; //#2 int main() { Foo<Bar>::foo(); } [clang] as well as [gcc] both seem to use user provided deduction guides (#2) when deducing the template parameter of template template parameter (#1). Is it

g++ c++17 class template argument deduction not working in a very specific case

你。 提交于 2019-12-08 15:53:20
问题 I have the following code: template <class T> class lit { public: lit(T l) : val(l) {} T val; }; template <class T> class cat { public: cat(lit<T> const& a, lit<T> const& b) : a(a), b(b) {} lit<T> const& a; lit<T> const& b; }; template <class T> cat<T> operator+(lit<T> const& a, lit<T> const& b) { return cat(a, b); } int main() { auto r1 = cat((lit ('b')), lit('d')); // compiles auto r2 = (lit ('b')) + lit('d') ; // doesn't compile auto r3 = lit ('b') + lit('d') ; // compiles auto r4 = (lit (

Substitution failure with `std::function` and previously deduced template parameter - why?

孤街浪徒 提交于 2019-12-08 14:39:39
问题 Consider the following code: template <typename> struct S { }; void g(S<int> t); template <typename T> void f(T, std::function<void(S<T>)>); When attempting to invoke f(0, g); I get the following error: error: no matching function for call to 'f' f(0, g); ^ note: candidate template ignored: could not match 'function<void (S<type-parameter-0-0>)>' against 'void (*)(S<int>)' void f(T, std::function<void(S<T>)>); ^ live example on godbolt.org While I understand that generally the type of the std

Template deduction not working for function pointer reference

梦想与她 提交于 2019-12-08 07:33:26
问题 I'm using detours and I find that the cast that they use very ugly, so I've written a couple of template functions to do the casting for me. // Cast a function pointer to a void * template <typename RET_TYPE, typename...ARGs> void* fnPtrToVoidPtr(RET_TYPE(WINAPI * pOriginalFunction)(ARGs...)) { return (void*)pOriginalFunction; } // Cast a function pointer that is referencable to a void *& template <typename RET_TYPE, typename...ARGs> void*& fnPtrRefToVoidPtrRef(RET_TYPE(WINAPI*&

Template deduction not working for function pointer reference

帅比萌擦擦* 提交于 2019-12-08 01:31:23
I'm using detours and I find that the cast that they use very ugly, so I've written a couple of template functions to do the casting for me. // Cast a function pointer to a void * template <typename RET_TYPE, typename...ARGs> void* fnPtrToVoidPtr(RET_TYPE(WINAPI * pOriginalFunction)(ARGs...)) { return (void*)pOriginalFunction; } // Cast a function pointer that is referencable to a void *& template <typename RET_TYPE, typename...ARGs> void*& fnPtrRefToVoidPtrRef(RET_TYPE(WINAPI*& pOriginalFunction)(ARGs...)) { return (void*&)pOriginalFunction; } This allows me to do the following call: BOOL