基于静态条件延迟对非嵌套类型的引用,且没有编译错误

Delay reference to non-nested type based on static condition without compile error?

本文关键字:引用 错误 编译 嵌套类型 于静态 静态 延迟 条件      更新时间:2023-10-16

考虑一个头文件,其内容为

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>();
}