基于静态条件延迟对非嵌套类型的引用,且没有编译错误
Delay reference to non-nested type based on static condition without compile error?
考虑一个头文件,其内容为
namespace foo
{
static bool const exists = false;
}
或
namespace foo
{
static bool const exists = true;
typedef some_other_possibly_incomplete_type my_type;
}
(假设这个头文件是按原样给我的,不能修改)
现在考虑这个typedef:
typedef get_my_type_or_default<foo::exists, void>::type my_type_or_default;
目标是如果foo::exists
,则my_type_or_default
求值为foo::my_type
,否则求值为void
。
是否有可能以一种方式定义get_my_type_or_default
使其工作,或者这是不可能的?如果这是可能的,我该怎么做?
使用奇怪的名称查找技巧,不幸地污染了全局命名空间:(
namespace foo
{
//static bool const exists = true; // we don't need this
struct some_other_possibly_incomplete_type;
//typedef some_other_possibly_incomplete_type my_type;
}
using my_type = void;
namespace foo
{
using this_one_surely_exists = my_type; // either foo::my_type or ::my_type
}
#include <iostream>
template<class T>
void print_type()
{ std::cout << __PRETTY_FUNCTION__ << "n"; }
int main()
{
using gey_my_type_or_default = foo::this_one_surely_exists;
print_type<gey_my_type_or_default>();
}
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++引用错误
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 只有级联分类器会发出未定义的引用错误
- 2个模板化类的非模板友元函数未定义引用错误
- C/C++ 取消引用错误:在空检查之前取消引用
- 为什么我的C++代码无法编译,出现未定义的引用错误
- 我的代码中C++未定义的引用错误?
- 模板专用化会导致未定义的引用错误
- .cpp文件中未定义的引用错误
- c++ 中目录中文件的未定义引用错误
- Opencv4 使用 pkg-config 给出未定义的引用错误
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- 共享指针取消引用错误
- 编译简单的多文件类程序时出现未定义的引用错误
- 模板函数产生未定义的引用错误
- 调用提取重加载器会产生对"运算符"的未定义引用错误>>
- 如何修复"未定义的引用"错误 opencv 和 g++
- 未定义的引用错误,我该怎么办?