我有以下代码:
#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
相关?!?!)。它发生在最后一行,当我明确指定类型时 double
调用operator<<
.
虽然在 clang++
上有效不管-std=c++11
标志,还有 g++
没有 -std=c++11
旗帜。这里发生了什么?我真的不知道,我是否以某种方式重载了全局 <<
在 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,
^
最佳答案
我认为归结为:
#include <type_traits>
template<class T>
struct foo
{
static_assert(std::is_integral<T>{}, "T must be integral");
};
template<class T> struct bar {};
template<class T, class Stream>
Stream& operator<<(Stream& p, foo<T> const&);
template<class T, class Stream>
Stream& operator<<(Stream& p, bar<T> const&);
int main()
{
int x;
operator<< <double>(x, 1.0);
}
请注意,在 libstdc++ 的实现中,第一个模板参数显式传递给了 operator<<
用作分发的模板参数(在 OP 中)。所以理论上,operator<<
的第二个函数参数的隐式转换可以调用分发类型。
在检查歧义之前,重载解析必须实例化类模板以检查转换构造函数:如果有,如果它们是显式的等等——也就是说,如果转换可行.
然而,为非整数类型实例化类模板是一个错误,由 static_assert
检查.因此,在选择其中一个重载之前,编译器会提示实例化。
如果编译器不需要它来选择重载,则不严格要求此实例化,请参阅 [temp.inst]/7。所有其他重载需要用户定义的转换(参数类型是类并且不同于参数类型),而在 OP 中定义为友元函数的重载是精确匹配。因此不需要检查那些其他转换的可行性,无论如何都不会选择那些重载。
不实例化不需要的类模板是某种优化——它不是强制性的。 clang++ 似乎在这里执行它,而 g++ 没有(并实例化类模板)。有关另一个示例和一些讨论,请参阅 C++ inconsistency between gcc and clang
关于c++ - ostream& operator<< 令人费解的编译错误的显式模板参数规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24852309/