ostream& amp;显式模板参数规范运营商LT;<令人费解的编译错误

时间:2014-07-20 16:03:18

标签: c++ c++11 g++

我有以下代码:

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

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,
       ^

1 个答案:

答案 0 :(得分:4)

我认为可以归结为:

#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