c++ - 在函数调用中,为什么 nullptr 不匹配指向模板对象的指针?

标签 c++ c++11 function-call template-matching nullptr

这是一个完美运行的代码示例:


#include<iostream>
#include<vector>

template< class D, template< class D, class A > class C, class A = std::allocator< D > >
void foo( C< D, A > *bar, C< D, A > *bas ) {
  std::cout << "Ok!" << std::endl;
}

int main( ) {
  std::vector< int > *sample1 = nullptr;
  std::vector< int > *sample2 = nullptr;
  foo( sample1, sample2 );
  return( 0 );
}

但是,在下面的代码中,编译器无法将 std::vector< int >* 与第二个参数的 nullptr 匹配,甚至无法从第一个参数中扣除模板类型。


#include<iostream>
#include<vector>

template< class D, template< class D, class A > class C, class A = std::allocator< D > >
void foo( C< D, A > *bar, C< D, A > *bas ) {
  std::cout << "Ok!" << std::endl;
}

int main( ) {
  std::vector< int > *sample = nullptr;
  foo( sample, nullptr );
  return( 0 );
}

错误信息是:


$ g++ -std=c++11 nullptr.cpp -o nullptr

nullptr.cpp: In function ‘int main()’:

nullptr.cpp:11:24: error: no matching function for call to ‘foo(std::vector<int>*&, std::nullptr_t)’

   foo( sample, nullptr );

nullptr.cpp:11:24: note: candidate is:

nullptr.cpp:5:6: note: template<class D, template<class D, class A> class C, class A> void foo(C<D, A>*, C<D, A>*)

 void foo( C< D, A > *bar, C< D, A > *bas ) {

nullptr.cpp:5:6: note:   template argument deduction/substitution failed:

nullptr.cpp:11:24: note:   mismatched types ‘C<D, A>*’ and ‘std::nullptr_t’

   foo( sample, nullptr );

为什么会这样?

最佳答案

这就是模板推导的工作原理:没有转换发生。

这个问题也不是 nullptr 特有的,请考虑极其简单的情况:

#include <iostream>

struct Thing {
    operator int() const { return 0; }
} thingy;

template <typename T>
void print(T l, T r) { std::cout << l << " " << r << "\n"; }

int main() {
    int i = 0;
    print(i, thingy);
    return 0;
}

哪个yields :

prog.cpp: In function ‘int main()’:
prog.cpp:12:17: error: no matching function for call to ‘print(int&, Thing&)’
  print(i, thingy);
                 ^
prog.cpp:12:17: note: candidate is:
prog.cpp:8:6: note: template<class T> void print(T, T)
 void print(T l, T r) { std::cout << l << " " << r << "\n"; }
      ^
prog.cpp:8:6: note:   template argument deduction/substitution failed:
prog.cpp:12:17: note:   deduced conflicting types for parameter ‘T’ (‘int’ and ‘Thing’)
  print(i, thingy);
                 ^

因此,nullptrint* 的转换也不会在模板参数推导之前发生。如前所述,您有两种解决问题的方法:

  • 指定模板参数(因此不会发生扣除)
  • 自己转换参数(推导发生,但在您显式转换之后)

关于c++ - 在函数调用中,为什么 nullptr 不匹配指向模板对象的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20405569/

相关文章:

c++ libcurl - 使用前一个 curl 的响应作为另一个 curl 的参数

c++ - Qt QFileDialog 再次弹出

C++使用类将值传递给函数

c++ - 如何将 constexpr 函数的参数标记为未使用?

javascript - 如何使用三元运算符选择函数

c++ - 当比较两个不同的字符串时,C++ 正在比较什么?

c++ - 如何使基类的某些构造函数只能由特定的派生类调用?

c++ - 进一步的右值引用和临时对象

javascript - 函数调用者无效元素

c - 如何更改 C 函数中的结构变量?