为什么/ Zc:twoPhase-编译器开关对MSVC没有影响?

时间:2018-05-14 14:50:05

标签: c++ templates visual-studio-2017

为什么/Zc:twoPhase-开关之后的/permissive-编译器开关导致编译错误在MSVC 19.13中消失,就像它们在完全删除/ permissive -`开关时消失一样,有以下问题代码?

#include <stdio.h>

template <unsigned int BYTES>
class CBase
{
public:
    char Arr[BYTES];

    int Fn1(void) {
        return Arr[1] ^ Arr[sizeof(Arr)-1];
    }

    int Fn2(void) {
        return Arr[2] ^ Arr[sizeof(Arr)-2];
    }
};

template <unsigned int BYTES>
class CDerived : public CBase<BYTES>
{
public:

    int FnSum(void) {
        return Fn1() + Fn2() + Arr[0];  // ERRORs: identifiers "Fn1" and "Fn2" and "Arr" are NOT found !
    }
};    

int main(void)
{
    CDerived<32> ddd;

    printf("%d\n", ddd.Fn1());  //No error here
    printf("%d\n", ddd.Fn2());  //No error here
    printf("%d\n", ddd.FnSum());

    return (int)ddd.Arr[0];   //No error here
}

完全删除/permissive-开关后,上述代码在MSVC v19.10中编译 请参阅:https://godbolt.org/g/Yxw89Y

注意:我不能在http://godbolt.org上添加/permissive- /Zc:twoPhase-开关的示例链接,因为最新的Godbolt MSVC编译器(v19.10)已过时且不支持/Zc:twoPhase-编译器切换。

根据 this article this article ,MSVC中的编译错误来自通过符合的两阶段名称查找C ++标准模式(由/permissive-选项启用) 此外,根据former article:&#34; /permissive-选项隐式设置符合的两阶段查找编译器行为,但可以使用/Zc:twoPhase- switch <覆盖它/ EM>&#34 ;.
但是,添加两个编译器开关/permissive- /Zc:twoPhase-(按此顺序)不会覆盖它,也不会使编译错误在MSVC v19.13中消失。

有关此问题的背景信息,请参阅 this entry

1 个答案:

答案 0 :(得分:0)

This code does build with the VS 2017 (15.7 update) version 19.14 when using /permissive-, so it was likely just a complier bug in VS 2017 (15.6 update) that has been fixed. You original code doesn't have anything to do with two-phase lookup.

If code errors out with /permissive-, and then builds with /permissive- /Zc:twoPhase- then it's two-phase related. Otherwise, it's just due to the conformance enforced by the /permissive- switch. See Give Visual C++ a Switch to Standard Conformance and Two-phase name lookup support comes to MSVC.

UPDATE It builds with x86 but not x64, which seems to reinforce the idea that this is a compiler bug and not a source conformance issue.