- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下带有自定义 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/
我看到微软的 stddef.h 定义 nullptr_t 是这样的: namespace std { typedef decltype(__nullptr) nullptr_t; } usin
我无法从 C++11 标准中判断 nullptr_t 是否具有默认构造函数。换句话说,以下是否有效?: nullptr_t n; GCC 和 VC++ 允许使用上述代码,但 clang 不允许。我在标
当使用文字 0 时在 C++ 中,编译器无法区分指针和 nullptr_t函数的重载。 说明问题的代码: struct Bar {}; void foo(Bar*) { std::cout #i
第 4.10/1 N3797 节说: A null pointer constant is an integer literal (2.14.2) with value zero or a prval
我有以下带有自定义 Variant 类和自定义 SmartPtr 类的代码: using namespace std; class Object { public: }; template class
以下 C++11 程序在 gcc 4.7.2 下不输出任何内容: #include using namespace std; decltype(nullptr) g() { cout op
引用 C++11:(18.2/9) nullptr_t is defined as follows: namespace std { typedef decltype(nullptr) nullptr
这是nullptr_t的声明在 : namespace std { typedef decltype(nullptr) nullptr_t; } 根据 this , std::nullptr_t是
我了解到nullptr除了可以转换为任何指针类型(但不能转换为任何整数类型)之外,还有自己的类型std::nullptr_t。所以有可能有一个接受 std::nullptr_t 的方法重载。 究竟为什
今天才知道C++允许std::nullptr_t:类型的非类型模板参数 template struct A { }; template void f() { } 在我的一生中,我无法为这些提出任何明智
使用 C++20 的 concept s 我注意到 std::unique_ptr 似乎无法满足 std::equality_comparable_with 概念。从 std::unique_ptr
我正在为游戏制作组件实体系统。 我有一个类,Object,它只是一个用于容纳组件的外壳,这些组件又作为 (typeid, unique_ptr<>) 对存储在 std::map 中的对象中。每个组件都
最初该项目编译并运行良好。然后,在不修改 setuptab.h|.cpp 文件的情况下,我试图避免创建 setuptab 的新实例,所以我尝试将其传递过去。 错误 No matching constr
我有一个启用的模板函数(通过 std::enable_if),它的参数是一个原始指针,或者有一个 std::iterator 类别或者是一个 std::nullptr_t。在该函数中,原始指针(数据成
这个问题在这里已经有了答案: Why is std::is_assignable counter-intuitive? (3 个答案) 关闭 5 年前。 cout ::value ::value <
显然 std::nullptr_t 参数在没有参数传递时被转换为 void * 类型的空指针(N3337 的第 5.2.2/7 节) (通过 ...)。这意味着要正确传递空 char * 指针,例如,
长话短说: 为什么模板化函数不能访问非模板化函数可以访问的相同转换? struct A { A(std::nullptr_t) {} }; template A makeA(T&& arg)
考虑以下代码: #include using namespace std; void fun(const char* s){ if (s == nullptr) { puts
这段代码: #include #include using namespace std; void dump(const std::string& s) { cout &) {
有人正在编译我使用 C++11 的 Qt 程序标准,他们得到了这个错误(Mac OS X/gcc)。我知道我可以声明它,但它不应该已经在 中了吗? ? ./collectable_smartptr.
我是一名优秀的程序员,十分优秀!