Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++ 当0或NULL通过模板间接传递给共享指针(共享\u ptr或唯一\u ptr)时,调用函数失败_C++_C++11_C++14 - Fatal编程技术网

C++ 当0或NULL通过模板间接传递给共享指针(共享\u ptr或唯一\u ptr)时,调用函数失败

C++ 当0或NULL通过模板间接传递给共享指针(共享\u ptr或唯一\u ptr)时,调用函数失败,c++,c++11,c++14,C++,C++11,C++14,编译失败,出现以下错误 #include<iostream> #include <memory> #include <thread> #include <mutex> using namespace std; class Linux { }; int f1(std::shared_ptr<Linux> spw) // call these only when { //do something return 0; } double

编译失败,出现以下错误

#include<iostream>
#include <memory>
#include <thread>
#include <mutex>
using namespace std;
class Linux
{
};
int f1(std::shared_ptr<Linux> spw) // call these only when
{
  //do something
  return 0;
}
double f2(std::unique_ptr<Linux> upw) // the appropriate
{
  //do something
  return 0.0;
}
bool f3(Linux* pw) // mutex is locked
{

return 0;
}

std::mutex f1m, f2m, f3m; // mutexes for f1, f2, and f3
using MuxtexGuard = std::lock_guard<std::mutex>;

template<typename FuncType, typename MuxType, typename PtrType>
auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
//decltype(auto) lockAndCall(FuncType func, MuxType& mutex, PtrType ptr)
{
        MuxtexGuard g(mutex);
        return func(ptr);
}
int main()
{
        auto result1 = lockAndCall(f1, f1m, 0); //compilation failed 
        //do something
        auto result2 = lockAndCall(f2, f2m, NULL); //compilation failed
        //do something
        auto result3 = lockAndCall(f3, f3m, nullptr);
        //do something
        return 0;
}
$g++nullptr.cpp--std=c++11
nullptr.cpp:在函数“int main()”中:
nullptr.cpp:39:49:错误:对“lockAndCall(double(&)(std::unique_ptr),std::mutex&,NULL)”的调用没有匹配的函数
auto result2=lockAndCall(f2,f2m,NULL)//编译失败
^
nullptr.cpp:39:49:注:候选人为:
nullptr.cpp:29:6:注意:模板decltype(func(ptr))lockAndCall(FuncType,MuxType&,PtrType)
自动锁定和调用(FuncType func、MuxType&mutex、PtrType ptr)->decltype(func(ptr))
^
nullptr.cpp:29:6:注意:模板参数扣除/替换失败:
nullptr.cpp:替换“模板decltype(func(ptr))lockAndCall(FuncType,MuxType&,PtrType)[其中FuncType=double(*)(std::unique\u ptr);MuxType=std::mutex;PtrType=long int]:
nullptr.cpp:39:49:从此处开始需要
nullptr.cpp:29:82:错误:无法将“ptr”从“long int”转换为“std::unique\u ptr”
自动锁定和调用(FuncType func、MuxType&mutex、PtrType ptr)->decltype(func(ptr))
问题1。当参数的0或空f1(std::shared_ptr spw)和f2(std::unique_ptr upw)以及函数调用
f1(static_cast(0))时,为什么lockAndCall调用失败
自动结果=f2(静态_转换(NULL))使用0和NULL成功调用


问题2lockAndCall函数扣除后FuncType的类型是什么?FuncType是函数指针或std::function的类型?

所以您的问题是函数声明为:

$ g++ nullptr.cpp  --std=c++11
nullptr.cpp: In function ‘int main()’:
nullptr.cpp:39:49: error: no matching function for call to ‘lockAndCall(double (&)(std::unique_ptr<Linux>), std::mutex&, NULL)’
         auto result2 = lockAndCall(f2, f2m, NULL); //compilation failed
                                                 ^
nullptr.cpp:39:49: note: candidate is:
nullptr.cpp:29:6: note: template<class FuncType, class MuxType, class PtrType> decltype (func(ptr)) lockAndCall(FuncType, MuxType&, PtrType)
 auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
      ^
nullptr.cpp:29:6: note:   template argument deduction/substitution failed:
nullptr.cpp: In substitution of ‘template<class FuncType, class MuxType, class PtrType> decltype (func(ptr)) lockAndCall(FuncType, MuxType&, PtrType) [with FuncType = double (*)(std::unique_ptr<Linux>); MuxType = std::mutex; PtrType = long int]’:
nullptr.cpp:39:49:   required from here
nullptr.cpp:29:82: error: could not convert ‘ptr’ from ‘long int’ to ‘std::unique_ptr<Linux>’
 auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
看起来您的实现将
NULL
定义为文本0,而不是
nullptr

要解决问题,请显式传递
nullptr
,而不是0或
NULL
,或者更好的默认构造
std::shared\u ptr
std::unique\u ptr

 #define NULL 0
 //since C++11
 #define NULL nullptr
lockAndCall(f1,f1m,std::shared_ptr{});

你能提供一个最小的复制样本吗?这是一个有趣的问题,但请用复制问题的最小程序来回答你的问题。你的问题一团糟,很多细节都不相关,也不清楚你在问什么为什么要问这个问题?你有什么建议吗?还是复制品?我喜欢匿名downvoters@Ajayyadav它可以定义为整数0或
nullptr
。无论如何,您的代码不应该依赖于它。@Ajayyadav是的,如果您将
constexpr
const
添加到
int
中,它必须是编译时常量again@black为什么?这就是cppreference.com所说的。@Ajayyadav提出了一个问题,为什么const/constexpr int不起作用
int f1(std::shared_ptr<Linux> spw);
f1(static_cast<int>(0));
template<typename FuncType, typename MuxType, typename PtrType>
auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
{
        MuxtexGuard g(mutex);
        return func(ptr);
}

lockAndCall( f1, f1m, 0);
 #define NULL 0
 //since C++11
 #define NULL nullptr
lockAndCall( f1, f1m, std::shared_ptr<Linux>{} );