lundi 30 décembre 2019

How can I prevent C++ for guessing a second template argument?

I'm using a C++ library (strf) which, somewhere within it, has the following code:

namespace strf {
template <typename ForwardIt, typename CharT>
inline auto range(ForwardIt begin, ForwardIt end, const CharT* sep) { /* ... */ }

template <typename Range, typename CharT>
inline auto range(const Range& range, const CharT* sep) { /* ... */ }
}

Now, I want to use strf::range<const char*>(some_char_ptr, some_char_ptr + some_length) in my code. But if I do so, I get the following error (with CUDA 10.1's NVCC):

error: more than one instance of overloaded function "strf::range" matches the argument list:
            function template "auto strf::range(ForwardIt, ForwardIt)"
            function template "auto strf::range(const Range &, const CharT *)"
            argument types are: (util::constexpr_string::const_iterator, util::constexpr_string::const_iterator)

I realize that the library code can probably be changed to avoid this (perhaps an std::enable_if to ensure Range is not a pointer?) ; but I can't change that. Instead, I want to somehow indicate to the compiler that I really really mean to only have one template argument, not one specified and another one deduced.

Can I do so?

Would appreciate answers for C++11 and C++14; C++17 answers involving deduction guides are less relevant but if you have one, please post it (for future NVCC versions...)

Aucun commentaire:

Enregistrer un commentaire