作为模板非类型参数的 C 字符串在 gcc 6.3 中有效,但在 Visual Studio 2017(x64 为 19
C string as template non-type parameter works in gcc 6.3 but does not work in Visual Studio 2017 (19.16.27027.1 for x64)
以下代码:
#include <iostream>
template<const char* Pattern> void f() {
std::cout << Pattern << "n";
}
static constexpr const char hello[] = "Hello";
int main() {
f<hello>(); //Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// string-as-template-parameter.cpp
// string-as-template-parameter.cpp(10): fatal error C1001: An internal error has occurred in the compiler.
// (compiler file 'msc1.cpp', line 1518)
// To work around this problem, try simplifying or changing the program near the locations listed above.
// Please choose the Technical Support command on the Visual C++
return 0;
}
当由 gcc (g++ (Debian 6.3.0-18+deb9u1( 6.3.0 20170516( 编译时工作,但在由 VS 2017 编译时会产生 C1001。
作为解决方法,我使用:
#include <iostream>
template<const char** Pattern> void f() {
std::cout << *Pattern << "n";
}
static const char* hello = "Hello";
int main() {
f<&hello>();
return 0;
}
有人有更漂亮的解决方案吗?可能是初始代码有一个被 gcc 跳过的错误?
有人有更漂亮的解决方案吗?
可以改用对std::string
的引用。
#include <iostream>
#include <string>
template<std::string & Pattern> void f() {
std::cout << Pattern << "n";
}
static std::string hello = "Hello";
int main() {
f<hello>();
return 0;
}
这在Visual Studio中使用MSVC编译。
这是有效的,因为根据 Cpp首选项,允许将带有链接的命名左值引用作为非类型参数。(请注意,hello
不是本地的。
相关文章:
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 代码在Visual Studio 2017中不起作用,但在VS代码中工作
- 默认/样板代码在Visual Studio 2017中给我错误.E1574.虚幻.但构建成功了
- 在Visual Studio中使用括号时编译错误,但在G ++ / CLang ++中传递
- 相同的代码在 Dev C++ 上编译,但在 Visual Studio 2017 上不编译
- 作为模板非类型参数的 C 字符串在 gcc 6.3 中有效,但在 Visual Studio 2017(x64 为 19
- 我的C 程序在CodeBlocks中提供了正确的结果,但在Visual Basic 2005 Express Edi
- 在 C++ Eclipse 中未定义对(错误)的引用,但在 Visual Studio 2015 中工作
- 为什么我的 c++ 程序在 Eclipse 中没有输出而终止,但在 Visual Studio 中工作
- 变量args SFINAE默认构造函数在clang中工作,但在Visual Studio 2015中失败
- 类模板继承无法在 GCC 中编译,但在 Visual Studio 中工作
- 在 G++ Ubuntu 中工作,但在 Visual Studio 中显示分段错误,在 Mac 中的代码块和 G++ 中
- 尝试编译OpenCV程序,但在Visual Studio 2012中出现LNK2038错误
- cpp文件不使用cl编译,但在Visual Studio中编译得很好
- g++编译器为表达式提供<<类型错误,但在Visual Studio中工作
- 64位exe在visual studio外崩溃,但在visual studio内工作
- 指向对象数组的c++指针在gnu c++中工作,但在visual studio中不工作
- C++类型转换运算符代码,在 Visual Studio 2012 中无法编译,但在 Visual Studio 2005 中工作正常
- 删除数组在CodeBlocks上有效,但在Visual上无效