小编ole*_*leg的帖子

C字符串作为模板非类型参数可在gcc 6.3中使用,但不适用于Visual Studio 2017(x64为19.16.27027.1)

如下代码:

#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)

c++ templates visual-studio

6
推荐指数
1
解决办法
144
查看次数

对std :: cout的引用会导致段错误

以下代码导致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()调用的行被评论时,段错误就会消失.

c++

3
推荐指数
1
解决办法
67
查看次数

标签 统计

c++ ×2

templates ×1

visual-studio ×1