mardi 29 décembre 2015

why type deduction failed 0 to shared_ptr and NULL to unique_ptr?

#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>;

void lockAndCallF1()
{
        MuxtexGuard g(f1m); // lock mutex for f1
        auto result = f1(static_cast<int>(0)); // pass 0 as null ptr to f1
        cout<< result<<endl;
}

void lockAndCallF2()
{
        MuxtexGuard g(f2m); // lock mutex for f2
        auto result = f2(static_cast<int>(NULL)); // pass NULL as null ptr to f2
        cout<< result<<endl;
}

void lockAndCallF3()
{
        MuxtexGuard g(f3m); // lock mutex for f2
        auto result = f3(nullptr);// pass nullptr as null ptr to f3 
        cout<< result<<endl;
} // unlock 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()
{
        lockAndCallF1();
        lockAndCallF2();
        lockAndCallF3();
        auto result1 = lockAndCall(f1, f1m, 0); //error - compilation failed
        auto result2 = lockAndCallF2(f2, f2m, NULL); //error  compilation failed
        auto result3 = lockAndCall(f3, f3m, nullptr);
        return 0;
}

compilation failed of above program failed

c++$ g++ nullptr.cpp  --std=c++11
nullptr.cpp: In function ‘int main()’:
nullptr.cpp:60:46: error: no matching function for call to ‘lockAndCall(int (&)(std::shared_ptr<Linux>), std::mutex&, int)’
         auto result1 = lockAndCall(f1, f1m, 0); //error
                                              ^
nullptr.cpp:60:46: note: candidate is:
nullptr.cpp:49: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:49: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 = int (*)(std::shared_ptr<Linux>); MuxType = std::mutex; PtrType = int]’:
nullptr.cpp:60:46:   required from here
nullptr.cpp:49:82: error: could not convert ‘ptr’ from ‘int’ to ‘std::shared_ptr<Linux>’
 auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
                                                                                  ^
nullptr.cpp:61:51: error: too many arguments to function ‘void lockAndCallF2()’
         auto result2 = lockAndCallF2(f2, f2m, NULL); //error 
                                                   ^
nullptr.cpp:35:6: note: declared here
 void lockAndCallF2()
      ^
nullptr.cpp:61:51: error: ‘void result2’ has incomplete type
         auto result2 = lockAndCallF2(f2, f2m, NULL); //error 

Question1. Why lockAndCall call is failed for 0 and Null and succeed for f1(static_cast<int>(0)); and auto result = f2(static_cast<int>(NULL));? Question2. I know it is deducted as int and i passing integer literal but i am typecasting to integer then even why it is failed?

Aucun commentaire:

Enregistrer un commentaire