尝试在 GCC 4.8 中编译以下代码:
#include <iostream>
#include <string>
template<class T>
class Bar {
public:
std::string test = "test";
};
int main() {
Bar<int> bar;
return 0;
}
使用g++ --std=c++11 -o main main.cpp
给出以下错误:
main.cpp: In constructor ‘constexpr Bar<int>::Bar()’:
main.cpp:5:7: error: conversion from ‘const char [5]’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested
class Bar {
^
main.cpp: In function ‘int main()’:
main.cpp:11:14: note: synthesized method ‘constexpr Bar<int>::Bar()’ first required here
Bar<int> bar;
^
但是,它在以下情况下可以正确编译:
- 使用 GCC 5.2
- 替换
std::string
为int
(并在右侧使用整数值) - 不使用模板类
它是 GCC 4.8 中的错误吗?我找不到在我的服务器上安装 GCC 5.2 的方法(我需要为 CentOS 6.8 编译它)所以我相信我被困在这里。