Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 预处理器check constexpr const char*函数_C++_Constexpr_Preprocessor - Fatal编程技术网

C++ 预处理器check constexpr const char*函数

C++ 预处理器check constexpr const char*函数,c++,constexpr,preprocessor,C++,Constexpr,Preprocessor,我正在使用一个新版本的库,该库将其版本作为静态constexpr const char*函数提供。我在测试时在新版本和旧版本之间切换,并希望在预处理器检查中包装仅在版本6.0中编译的代码 下面我模拟了库如何显示版本号,并尝试检测它是否以'6'开头: #include <iostream> // This comes from a library static constexpr const char* VersionNumber() { return "6.0"; } // Th

我正在使用一个新版本的库,该库将其版本作为
静态constexpr const char*
函数提供。我在测试时在新版本和旧版本之间切换,并希望在预处理器检查中包装仅在版本6.0中编译的代码

下面我模拟了库如何显示版本号,并尝试检测它是否以
'6'
开头:

#include <iostream>

// This comes from a library
static constexpr const char* VersionNumber() { return "6.0"; }

// This would be my intended method of detecting the version number
static constexpr bool isVersionSix() { return VersionNumber()[0] == '6'; }

int main() {
    std::cout << "Code for any version\n";

#if isVersionSix()
    std::cout << "Additional code for version 6.0\n";
#endif

    return 0;
}
我怎样才能避开这件事?另一方面,我认为检查
'6'
而不是
“6.0”
会更容易,如果可以进行编译,那么创建
constepr
字符串比较函数应该不会有问题

我怎样才能避开这件事

选项1

您可以在运行时测试版本,并调用合适的函数或代码块

int main() {
    std::cout << "Code for any version\n";

    if ( isVersionSix() )
    {
       std::cout << "Additional code for version 6.0\n";
    }

    return 0;
}
intmain(){
标准::cout
我怎样才能避开这件事

选项1

您可以在运行时测试版本,并调用合适的函数或代码块

int main() {
    std::cout << "Code for any version\n";

    if ( isVersionSix() )
    {
       std::cout << "Additional code for version 6.0\n";
    }

    return 0;
}
intmain(){

std::cout如果可以升级到GCC 7,则可以使用
If constexpr
,如下所示:

#include <iostream>

// This comes from a library
static constexpr const char* VersionNumber() { return "6.0"; }

// This would be my intended method of detecting the version number
static constexpr bool isVersionSix() { return VersionNumber()[0] == '6'; }

int main() {
    std::cout << "Code for any version\n";

    if constexpr (isVersionSix()) {
        std::cout << "Additional code for version 6.0\n";
    }

    return 0;
}
#包括
//这来自图书馆
静态constexpr const char*VersionNumber(){返回“6.0”}
//这将是我想要的检测版本号的方法
静态constexpr bool isVersionSix(){return VersionNumber()[0]=='6';}
int main(){

std::cout如果可以升级到GCC 7,则可以使用
If constexpr
,如下所示:

#include <iostream>

// This comes from a library
static constexpr const char* VersionNumber() { return "6.0"; }

// This would be my intended method of detecting the version number
static constexpr bool isVersionSix() { return VersionNumber()[0] == '6'; }

int main() {
    std::cout << "Code for any version\n";

    if constexpr (isVersionSix()) {
        std::cout << "Additional code for version 6.0\n";
    }

    return 0;
}
#包括
//这来自图书馆
静态constexpr const char*VersionNumber(){返回“6.0”}
//这将是我想要的检测版本号的方法
静态constexpr bool isVersionSix(){return VersionNumber()[0]=='6';}
int main(){


std::cout不可能使用预处理器来执行此操作,但您确实有选项(例如,如果内部代码可以在任何版本上编译,则只需使用
即可)@Justin,不幸的是,“6.0版的附加代码”对于版本6的要求,它不是在较低版本中编译的。只要找到一种方法,避免每次我交换库时使用手动预处理器条件,在C++ C++ 17上,你可以使用<代码>如果是CONTXPRP</代码>。在此之前,你可以使用sFIFE技巧创建一个<代码> doyv6i Works<代码>函数Wh。ich在版本6之前是空的。@DanielH,gcc 6.3.1 std=c++1zi如果您可以将编译器升级到7.1,那么最好的答案是使用
if constexpr(isVersionSix())
包装额外的代码。不幸的是,6.3不支持这一点。不可能使用预处理器来完成此操作,但您确实有选项(例如,如果里面的代码可以在任何版本上编译,只需使用
if
)@Justin,不幸的是,“6.0版的附加代码”对于版本6的要求,它不是在较低版本中编译的。只要找到一种方法,避免每次我交换库时使用手动预处理器条件,在C++ C++ 17上,你可以使用<代码>如果是CONTXPRP</代码>。在此之前,你可以使用sFIFE技巧创建一个<代码> doyv6i Works<代码>函数Wh。ich在版本6之前为空。@DanielH,gcc 6.3.1 std=c++1zi如果您可以将编译器升级到7.1,那么最好的答案是使用
if constexpr(isVersionSix())包装附加代码
。不幸的是,6.3不支持这一点。在C++17和C++20中会有更多选项。在C++17中,如果constexpr
,您可以使用
,尽管我认为条件需要依赖于模板参数。在C++20中,您可以使用
requires(isVersionSix())重载函数
,它应该可以工作。我不能使用第一个选项,因为代码不在较低版本中编译,尽管第二个选项可以作为单个代码块的包装器工作,但我可能需要在许多地方只显示特定版本的代码number@Justin,你相信吗?我仍在与VS2008合作:)@asimes,在这种情况下,您必须在所有位置使用选项2中的策略。在C++17和C++20中会有更多选项。在C++17中,如果constexpr
,您可以使用
,尽管我相信条件需要依赖于模板参数。在C++20中,您可以使用
requires(isVersionSix())重载函数
,它应该可以工作。我不能使用第一个选项,因为代码不在较低版本中编译,尽管第二个选项可以作为单个代码块的包装器工作,但我可能需要在许多地方只显示特定版本的代码number@Justin,你相信吗?我仍在与VS2008合作:)@asimes,在这种情况下,您必须在所有位置使用选项2中的策略。这正是我想要的,但我无法将编译器升级到7.1(工作时)现在我到家了,我已经有了gcc 7.2.0,我试过了。让我们先把版本号忘了,举个例子。如果我把没有编译的代码放在
#If 0
..
#endif
里面,它当然会编译。但是如果我把没有编译的代码放在
If constexpr(false){
..
}里面
那么它不会编译。这与我的问题有关,因为当库的版本号小于6.0时,库没有版本6所需的方法。0@asimes这取决于“不编译”的类型。进一步研究,规则比我想象的更严格,因此这不太可能像我预期的那样有用。它对于某些形式的不编译仍然有用,但对于这样的库可能不会。这正是我想要的,但我无法将编译器升级到7.1(工作中)现在我到家了,我已经有了gcc 7.2.0,我试过了。举个例子,让我们暂时忘记版本号。如果我把没有编译的代码放在里面