Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有非模板类型参数的模板函数的重载解析 #包括 #包括 #包括 名称空间详细信息 { 模板 constexpr bool是\u类型的\u左值\u引用\u= std::is_左值\参考::值&&std::is_相同::值; //容器是左值引用,没有筛选器、回显参数 模板 > 常量标准::向量和f(空*、容器和c) { 标准::cout> std::vector f(过滤器&,常量std::vector&) { 标准::cout_C++_Templates_C++14_Overloading_Sfinae - Fatal编程技术网

C++ 具有非模板类型参数的模板函数的重载解析 #包括 #包括 #包括 名称空间详细信息 { 模板 constexpr bool是\u类型的\u左值\u引用\u= std::is_左值\参考::值&&std::is_相同::值; //容器是左值引用,没有筛选器、回显参数 模板 > 常量标准::向量和f(空*、容器和c) { 标准::cout> std::vector f(过滤器&,常量std::vector&) { 标准::cout

C++ 具有非模板类型参数的模板函数的重载解析 #包括 #包括 #包括 名称空间详细信息 { 模板 constexpr bool是\u类型的\u左值\u引用\u= std::is_左值\参考::值&&std::is_相同::值; //容器是左值引用,没有筛选器、回显参数 模板 > 常量标准::向量和f(空*、容器和c) { 标准::cout> std::vector f(过滤器&,常量std::vector&) { 标准::cout,c++,templates,c++14,overloading,sfinae,C++,Templates,C++14,Overloading,Sfinae,当传递类型为void*,const std::vector&的参数时,是否有一种方法可以自动优先选择第一个模板重载,而不手动排除此组合的第二个重载 也许您可以添加第三个未使用的参数,第一个重载为int,第二个重载为long,并使用0(aint值)调用f(),以赋予第一个参数以优先级 下面是一个完整的示例 #include <type_traits> #include <vector> #include <iostream> namespace detail

当传递类型为
void*
const std::vector&
的参数时,是否有一种方法可以自动优先选择第一个模板重载,而不手动排除此组合的第二个重载

也许您可以添加第三个未使用的参数,第一个重载为
int
,第二个重载为
long
,并使用
0
(a
int
值)调用
f()
,以赋予第一个参数以优先级

下面是一个完整的示例

#include <type_traits>
#include <vector>
#include <iostream>

namespace detail
{
template <typename T, typename U>
constexpr bool is_lvalue_reference_of_type =
    std::is_lvalue_reference<T>::value && std::is_same<std::decay_t<T>, U>::value;

// container is lvalue reference and no filter, echo back parameter
template <typename Container,
          typename = std::enable_if_t<
                is_lvalue_reference_of_type<Container, std::vector<int>>
            >
          >
const std::vector<int>& f(void *, Container && c)
{
    std::cout << "void *\n";
    return c;
}
// filter input and return a copy
template <typename Filter,
          typename = std::enable_if_t<!std::is_same_v<std::decay_t<Filter>, void *>>>
std::vector<int> f(Filter &&, const std::vector<int> &)
{
    std::cout << "Filter \n";
    return {};
}
}


template <typename T = void*>
void g(T && t = nullptr)
{
    const std::vector<int> v;
    detail::f(std::forward<T>(t), v);
}

int main(int, const char * const * const)
{
    g();
    g([](const int) {return true;});
}
#包括
#包括
名称空间详细信息
{
模板
标准::向量常量和f(void*,容器和c,int)

{std::cout重载解析试图找到一个对所有参数都最匹配的函数。但显然,第一个函数与
void*
完全匹配。第二个函数对第二个参数更专业,与向量匹配。因此,没有最佳选择。
#include <vector>
#include <iostream>

namespace detail
 {
   template <typename Container>
   std::vector<int> const & f (void *, Container && c, int)
    { std::cout << "void *\n"; return c; }

   template <typename Filter>
   std::vector<int> f (Filter &&, std::vector<int> const &, long)
    { std::cout << "Filter \n"; return {}; }
 }


template <typename T = void*>
void g (T && t = nullptr)
 {
   std::vector<int> const v;
   detail::f(std::forward<T>(t), v, 0);
 }

int main ()
 {
   g();
   g([](int const) { return true; });
 }