Category : template-argument-deduction

I’ve been trying to implement some generic wrappers (similar to std::span). However, I can’t seem to get the template deduction/ conversion working. I’m not sure why. Below is a minimum "not-compiling" example #include <array> #include <iostream> template<typename T> class Wrapper { public: using ElementType = T; using ValueType = std::remove_cv<T>; using Pointer = ElementType*; constexpr ..

Read more

There is a custom allocator class: template<typename T> class pool_allocator { public: using value_type = T; using pointer = value_type *; /* Default constructor */ constexpr pool_allocator( void ) noexcept = default; /* Converting constructor used for rebinding */ template<typename U> constexpr pool_allocator( const pool_allocator<U> & ) noexcept {} [[nodiscard]] pointer allocate( size_t n, [[maybe_unused]] ..

Read more

The following snippet of C++17 code compiles in GCC and CLang, but in Visual C++ it gives the error C2784: Could not deduce template argument. https://godbolt.org/z/o4fe351Kn #include <iostream> #include <vector> template< template <typename…> typename container > std::ostream & f (std::ostream & out, const container< int > &) { return out; } std::ostream & operator << ..

Read more

Here is some code that compiles in GCC (on godbolt at least – can’t test locally), for handling a compile-time dependency system – the conversion operator here is to make it easier to take an entity that specifies what it can read/write and reduce it down implicitly to a more restricted form when passing into ..

Read more

I’m trying to create a function template which accepts std::max and others with compatible prototypes: template <typename F> int f(F g) { return g(1,2); } Because of the many overloads, the template argument F cannot be inferred f.e. for std::max, so this will fail: f(std::max); One possibility is to hint with a static_cast: f(static_cast<int const& ..

Read more

I’m trying to create a function template which accepts std::max and others with compatible prototypes: template <typename F> int f(F g) { return g(1,2); } Because of the many overloads, the template argument F cannot be inferred f.e. for std::max, so this will fail: f(std::max); One possibility is to hint with a static_cast: f(static_cast<int const& ..

Read more

I am confused about Template Argument Deduction with Forwarding reference; AFAIK Reference Collapsing occurs only when a forwarding (universal) reference is combined with another reference. I have this example: template <typename T> T&& fwd_(T& arg){ std::cout << "fwd_(type&)n"; return static_cast<T&&>(arg); } template <typename T> T&& fwd_(T&& arg){ std::cout << "fwd_(type&&)n"; ++arg; return static_cast<T&&>(arg); } void ..

Read more