Share via


/Zc:templateScope(检查模板参数阴影)

/Zc:templateScope 编译器选项针对模板参数的阴影启用标准 C++ 行为的检查。

语法

/Zc:templateScope[-]

备注

C++ 标准不允许在模板范围内重复使用模板参数的名称(或阴影)作为另一个声明。 /Zc:templateScope 编译器选项为此类阴影启用错误检查。

/Zc:templateScope 选项是 Visual Studio 2022 版本 17.5 预览 1 中新增的。 此选项默认处于关闭状态,即使代码是使用 /permissive- 选项(或暗示 /permissive-的选项,如 /std:c++20/std:c++latest)编译的。 若要显式启用错误检查,必须将 /Zc:templateScope 显式添加到编译器命令行。 若要显式禁用此检查,请使用 /Zc:templateScope- 选项。

示例

/Zc:templateScope下,此示例代码生成错误:

template<typename T>
void f(T&& t) {
    int T = 13;
}

/* Output:
t.cpp(3): error C7527: 'T': a template parameter name cannot be reused within its scope
*/

在 Visual Studio 中设置此编译器选项

  1. 打开项目的“属性页” 对话框。 有关详细信息,请参阅在 Visual Studio 中设置 C++ 编译器和生成属性

  2. 选择“配置属性”>“C/C++”>“命令行”属性页

  3. 其他选项中添加 /Zc:templateScope/Zc:templateScope-。 选择“确定”或“应用”以保存更改。

另请参阅

/Zc(一致性)
/permissive-
/std(指定语言标准版本)