ctad

Template argument deduction fails when using braced initializer list

妖精的绣舞 提交于 2021-01-27 19:03:17
问题 I'm trying to use template argument deduction in the 'perpendicular()' function: #include <iostream> template <typename component = double> struct offset { component x; component y; }; template <typename component> offset(component x, component y) -> offset<component>; template <typename component> offset<component> perpendicular(offset<component> const &o) { return offset{o.y, -o.x}; } template <typename component> std::ostream &operator<<(std::ostream &s, offset<component> const &o) {

Class Template Argument Deduction - clang and gcc differ

与世无争的帅哥 提交于 2020-07-20 17:04:12
问题 The following code compiles with gcc but not with clang: template<typename T> class number { T num; public: number(T num = 0): num(num) {} template<typename T1, typename T2> friend auto add(T1 a, T2 b); }; template<typename T1, typename T2> auto add(T1 a, T2 b) { // this compiles with both: // return number<T1>{a}.num + number<T2>{b}.num; // this compiles only with gcc: return number{a}.num + number{b}.num; // <== clang is unhappy here } int main() { auto result = add(1.0, 2.0); } Compilation

CTAD and designated initializers in C++20

只谈情不闲聊 提交于 2019-12-07 00:05:10
问题 I have already stated confusion about CTAD with designated initializers in this question, but i have another confusion with a very similar code snippet template <typename int_t=int, typename float_t=float> struct my_pair { int_t first; float_t second; }; template<typename ... ts> my_pair(ts...) -> my_pair<ts...>; int main() { my_pair x{.second = 20.f}; static_assert( std::is_same_v<decltype(x.first), int> ); //FAILS <- its deduced to float static_assert( std::is_same_v<decltype(x.second),

CTAD and designated initializers in C++20

回眸只為那壹抹淺笑 提交于 2019-12-05 05:36:25
I have already stated confusion about CTAD with designated initializers in this question , but i have another confusion with a very similar code snippet template <typename int_t=int, typename float_t=float> struct my_pair { int_t first; float_t second; }; template<typename ... ts> my_pair(ts...) -> my_pair<ts...>; int main() { my_pair x{.second = 20.f}; static_assert( std::is_same_v<decltype(x.first), int> ); //FAILS <- its deduced to float static_assert( std::is_same_v<decltype(x.second), float> ); } It seems like the deduction guide causes the type of first to be deduced to float , even