如下代码:
#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 …Run Code Online (Sandbox Code Playgroud) 以下代码导致gcc(Debian 6.3.0-18 + deb9u1)6.3.0 20170516上的段错误
#include <iostream>
template<typename LogT> class logger {
public:
logger(LogT& log) : log_(log) {}
template<typename T> LogT& operator<<(T const& t) {return log_ << "\n> " << t;}
private:
LogT& log_;
};
template<typename LogT> class A {
public:
void f() {
alog << "world";
}
static LogT& alog;
};
logger<std::ostream> alog(std::cout);
template<> logger<std::ostream>& A<logger<std::ostream>>::alog = alog;
int main() {
alog << "hello";
A<logger<std::ostream>>().f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?当带有f()调用的行被评论时,段错误就会消失.