我有以下带有自定义 Variant 类和自定义 SmartPtr 类的代码:
using namespace std;
class Object
{
public:
};
template<typename T>
class SmartPtr
{
public:
template<typename Y>
explicit SmartPtr(Y* p) { p_ = p; }
SmartPtr(std::nullptr_t) { p_ = nullptr; }
private:
T* p_;
};
class Variant
{
public:
Variant(bool b) : _b(b) { }
private:
bool _b;
};
class Obj
{
public:
void test(SmartPtr<Object> /*p*/) { cout << "smartptr version!" << endl; }
void test(Variant /*v*/) { cout << "variant version!" << endl; }
};
int main(int argc, const char *argv[])
{
Obj o;
o.test(nullptr); // calls SmartPtr version
o.test(true); // calls Variant version
o.test(false); // -> compiler error: ambiguous call to overloaded function
return 0;
}
我假设 bool 值 false 既可以转换为 Variant,也可以转换为 0,然后转换为 nullptr,然后转换为 SmartPtr,这会导致此错误。
是否有可能避免这种转换?
对于库的用户,API 与“o.test(true);”一起工作但需要类似 'o.test(Variant(false));' 的东西编译不是很直观。
最佳答案
我相信我有一个理想的解决方案。它只需要改变测试函数,所以它让 SmartPtr 和 Variant 保持独立,这是理想的。它添加了一个未定义的模板化重载以测试具有已定义的 bool 和 nullptr 的特化。这直接将 bool 和 nullptr 分派(dispatch)到所需的特化,但会导致其他未处理类型的链接错误。我很高兴能解决这个问题,因为我自己确实以多种形式遇到过这个问题。我希望你可以使用函数参数的显式!!
我的想法来自这里:C++ templates that accept only certain types
using namespace std;
class Object
{
public:
};
class Variant
{
public:
Variant( bool b) : _b(b) { }
private:
bool _b;
};
template<typename T>
class SmartPtr
{
public:
SmartPtr(std::nullptr_t null) { p_ = nullptr; }
template<typename Y>
SmartPtr(Y* p) { p_ = p; }
private:
T* p_;
};
class Obj
{
public:
void test(SmartPtr<Object> here /*p*/) {
cout << "smartptr version!" << endl;
}
void test(Variant /*v*/) { cout << "variant version!" << endl; }
template<typename T> void test(T t);
template<>
void test<bool>(bool b) {
cout << "bool specialization" << endl;
test(Variant(b));
}
template<>
void test<std::nullptr_t>(std::nullptr_t null) {
cout << "nullptr specialization" << endl;
test(SmartPtr<Object>(nullptr));
}
};
int main(int argc, const char *argv[])
{
Obj o;
Obj c;
Object object;
//o.test(3); // Gives link error LNK2019
o.test(Variant(true)); // calls Variant version
o.test(SmartPtr<Object>(&object)); // calls SmartPtr version
o.test(nullptr); // dispatched to SmartPtr version by nullptr specialization
o.test(true); // dispatched to Variant version by bool specialization
o.test(false); // dispatched to Variant version by bool specialization
return 0;
}
我已经回答了一些不太理想的问题,所以我将这个答案原封不动地保留如下:
=============================================
我在这里没有理想的解决方案,而且我不知道您的代码有哪些限制,因此这对您来说可能没有实际用途,但以下内容是明智的。它不允许代码在编译时使用 nullptr,并依赖于全局 null_smart 常量用于调用者只是对传递对象不感兴趣的所有情况。
#include <iostream>
using namespace std;
class Object
{
public:
};
class Variant
{
public:
Variant(bool b) : _b(b) { }
private:
Variant(std::nullptr_t) {};
private:
bool _b;
};
template<typename T>
class SmartPtr
{
public:
SmartPtr() { p_ = nullptr; }
template<typename Y>
SmartPtr(Y* p) { p_ = p; }
private:
T* p_;
};
class Obj
{
public:
void test(SmartPtr<Object> /*p*/) { cout << "smartptr version!" << endl; }
void test(Variant /*v*/) { cout << "variant version!" << endl; }
};
const SmartPtr<Object> null_smart;
int main(int argc, const char *argv[])
{
Obj o;
o.test(null_smart); // calls SmartPtr version, without interest in passing object
o.test(true); // calls Variant version
o.test(false); // calls Variant version
return 0;
}
它比 true/Variant(false) 问题更清晰,但仍然有点挑剔。
关于c++ - bool 自动转换为 nullptr_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25549169/