@H_301_4@istream_iterator and @H_301_4@istreambuf_iterator should support literal
sentinel values. The default constructor is frequently used to
terminate ranges,and could easily be a literal value for
@H_301_4@istreambuf_iterator,and @H_301_4@istream_iterator when iterating value
types. [Rest omitted]
这对我来说并没有什么意义.有人能告诉我一个他们的意思的例子吗?
N3308是另一篇提到但并不解释问题的论文:
Some of the @H_301_4@istream_iterator<T> constructors are required to be
@H_301_4@constexpr if @H_301_4@T is a literal type. The intention is to allow
existing implementation technique of storing a type of @H_301_4@T inline to
continue to work. [libstdc++ does this,@H_301_4@_Tp _M_value] However,it
actually rules out this technique: the default and copy constructors
of @H_301_4@T need not be marked @H_301_4@constexpr,and if they are not,the
@H_301_4@istream_iterator<T> constructors could not be instantiated as
@H_301_4@constexpr.
以上解释了这个琐碎的拷贝构造函数和析构函数,但是并不是为什么默认构造函数被标记为constexpr.
此外,在线GCC 5.2.0的测试中,我复制了libstdc的实现.唯一的改变是我从istream_iterator()中删除了constexpr.在这两种情况下,组件是相同的.
解决方法
// istream_iterator example #include <iostream> // std::cin,std::cout #include <iterator> // std::istream_iterator int main () { double value1,value2; std::cout << "Please,insert two values: "; std::istream_iterator<double> eos; // end-of-stream iterator std::istream_iterator<double> iit (std::cin); // stdin iterator if (iit!=eos) value1=*iit; ++iit; if (iit!=eos) value2=*iit; std::cout << value1 << "*" << value2 << "=" << (value1*value2) << '\n'; return 0; }
http://www.cplusplus.com/reference/iterator/istream_iterator/istream_iterator/
声明这个一个constexpr允许编译器将创建流终端迭代器的调用折叠成常量,而不是每次调用一个函数.否则可能会在循环的每次迭代中这样做.