【发布时间】:2014-07-20 16:03:17
【问题描述】:
我有以下代码:
#include <iostream>
//#include <algorithm> // compile error when `g++ -std=c++11`, fine otherwise
using namespace std;
template<typename T>
class Foo
{
T x_;
public:
Foo(const T& x={}): x_(x){}; // default ctor, bounded rvalue default-initialized
template<typename S>
friend ostream& operator<<(ostream& lhs, const Foo<S>& rhs)
{
return lhs << rhs.x_;
}
};
int main()
{
Foo<double> foo(10.1);
cout << foo << endl;
operator<<(cout, foo) << endl;
//puzzling compile-time error when #include<algorithm> and compile
// with `g++ -std=c++11`, otherwise fine (both clang++ and g++)
operator<< <double>(cout, foo) << endl;
}
一切都可以编译并且工作正常,但是,如果我 #include<algorithm> 并使用 g++ -std=c++11 编译,我会收到一个非常混乱的编译错误(见下文,与 std::uniform_int_distribution 相关?!?!)。它发生在最后一行,当我为调用operator<< 明确指定类型double 时。
不管-std=c++11 标志如何,它都可以在clang++ 上工作,也可以在没有-std=c++11 标志的g++ 上工作。这里发生了什么?我真的不知道,我是否以某种方式重载了algorithm 中定义的全局<<?
这是g++ 错误吗? (我使用 g++4.9 顺便说一句)。
In file included from /opt/local/include/gcc49/c++/random:49:0,
from /opt/local/include/gcc49/c++/bits/stl_algo.h:66,
from /opt/local/include/gcc49/c++/algorithm:62,
from /Users/vlad/minimal.cpp:2:
/opt/local/include/gcc49/c++/bits/random.h: In instantiation of 'class std::uniform_int_distribution<double>':
/Users/vlad/minimal.cpp:25:34: recursively required by substitution of 'template<class _IntType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::uniform_int_distribution<_IntType>&) [with _IntType = double; _CharT = char; _Traits = std::char_traits<char>]'
/Users/vlad/minimal.cpp:25:34: required from here
/opt/local/include/gcc49/c++/bits/random.h:1668:7: error: static assertion failed: template argument not an integral type
static_assert(std::is_integral<_IntType>::value,
^
/opt/local/include/gcc49/c++/bits/random.h: In instantiation of 'class std::geometric_distribution<double>':
/Users/vlad/minimal.cpp:25:34: recursively required by substitution of 'template<class _IntType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::geometric_distribution<_IntType>&) [with _IntType = double; _CharT = char; _Traits = std::char_traits<char>]'
/Users/vlad/minimal.cpp:25:34: required from here
/opt/local/include/gcc49/c++/bits/random.h:4010:7: error: static assertion failed: template argument not an integral type
static_assert(std::is_integral<_IntType>::value,
^
/opt/local/include/gcc49/c++/bits/random.h: In instantiation of 'class std::negative_binomial_distribution<double>':
/Users/vlad/minimal.cpp:25:34: recursively required by substitution of 'template<class _IntType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::negative_binomial_distribution<_IntType>&) [with _IntType = double; _CharT = char; _Traits = std::char_traits<char>]'
/Users/vlad/minimal.cpp:25:34: required from here
/opt/local/include/gcc49/c++/bits/random.h:4210:7: error: static assertion failed: template argument not an integral type
static_assert(std::is_integral<_IntType>::value,
^
/opt/local/include/gcc49/c++/bits/random.h: In instantiation of 'class std::poisson_distribution<double>':
/Users/vlad/minimal.cpp:25:34: recursively required by substitution of 'template<class _IntType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::poisson_distribution<_IntType>&) [with _IntType = double; _CharT = char; _Traits = std::char_traits<char>]'
/Users/vlad/minimal.cpp:25:34: required from here
/opt/local/include/gcc49/c++/bits/random.h:4432:7: error: static assertion failed: template argument not an integral type
static_assert(std::is_integral<_IntType>::value,
^
/opt/local/include/gcc49/c++/bits/random.h: In instantiation of 'class std::binomial_distribution<double>':
/Users/vlad/minimal.cpp:25:34: recursively required by substitution of 'template<class _IntType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::binomial_distribution<_IntType>&) [with _IntType = double; _CharT = char; _Traits = std::char_traits<char>]'
/Users/vlad/minimal.cpp:25:34: required from here
/opt/local/include/gcc49/c++/bits/random.h:3779:7: error: static assertion failed: template argument not an integral type
static_assert(std::is_integral<_IntType>::value,
^
/opt/local/include/gcc49/c++/bits/random.h: In instantiation of 'class std::discrete_distribution<double>':
/Users/vlad/minimal.cpp:25:34: recursively required by substitution of 'template<class _IntType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::discrete_distribution<_IntType>&) [with _IntType = double; _CharT = char; _Traits = std::char_traits<char>]'
/Users/vlad/minimal.cpp:25:34: required from here
/opt/local/include/gcc49/c++/bits/random.h:5253:7: error: static assertion failed: template argument not an integral type
static_assert(std::is_integral<_IntType>::value,
^
【问题讨论】: