如何防止C ++猜测第二个模板参数?

问题描述 投票:7回答:3

我正在使用C ++库(strf),该库中的某个位置具有以下代码:

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

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

现在,我想在代码中使用strf::range<const char*>(some_char_ptr, some_char_ptr + some_length)。但是,如果这样做,我将收到以下错误(使用CUDA 10.1的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)

library代码可以更改以避免这种情况(例如,使用:

inline auto range(const typename std::enable_if<not std::is_pointer<typename std::remove_cv<Range>::type>::value, Range &>::type range, const CharT* sep)

以确保Range不是指针);但我现在无法进行更改。相反,我想以某种方式向编译器指示,我实际上的意思是只具有一个模板参数,而不是指定一个并推导出另一个。

我能这样做吗?

[感谢C ++ 11和C ++ 14的答案;涉及推导指南的C ++ 17答案不太相关,但如果有,请张贴(对于将来的NVCC版本...)

我正在使用C ++库(strf),该库在其中的某个位置具有以下代码:namespace strf {template 内联自动范围(ForwardIt开始,ForwardIt结束){/ * ... * /} .. 。

c++ c++11 nvcc overload-resolution template-argument-deduction
3个回答
4
投票

5
投票

2
投票
© www.soinside.com 2019 - 2024. All rights reserved.