c++ - ostream& operator<< 令人费解的编译错误的显式模板参数规范

标签 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)。

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/

相关文章:

c++ - C++的动态代码分析

c++ - 从头到尾列出删除范围

c++ - 关于 __attribute__((__packed__)) 的 c++ 编译错误还有哪些其他方法?

c++ - 如何在 Visual Studio 2013 中为函数类型创建类型别名?

c++ - 这是对具有 C++ 继承层次结构的 namespace 的合理使用吗?

c++ - 对象数组 - std::array - 构造函数初始化问题

c++ - C++0x 中高阶函数和 lambda 的问题

R: 无法在 Ubuntu 14.04 上安装 'rasclass' 包

c++ - 编译过时的c++包: undefined reference errors

c++ - 启用 -msse、-msse2 和 -mfpmath=sse 总是会让我的程序运行得更快吗?