提问者:小点点

C++自定义范围类型MSVC编译,但G++不编译


我使用Visual Studio2019来学习C++,因为它是一个很棒的IDE,可以当场捕获错误。 我下面的程序没有显示错误,并且用MSVC编译得很好,但是当我尝试用G++10.1编译时,它就不能这样做了

#include <cstdio>

class FiboIterator {
    int current{ 1 };
    int last{ 1 };
public:
    bool operator!=(int x) const {
        return x >= current;
    }
    FiboIterator& operator++() {
        const auto tmp = current;
        current += last;
        last = tmp;
        return *this;
    }
    int operator*() {
        return current;
    }
};

class FiboRange {
    const int max;
public:
    explicit FiboRange(int max): max{max} {}
    FiboIterator begin() const {
        return FiboIterator{};
    }
    int end() const {
        return max;
    }
};

int main() {
    for (const auto i : FiboRange{ 5000 }) {
        printf("%d ", i);
    }
}

G++输出以下消息:

main.cpp: In function 'int main()':
main.cpp:34:38: error: inconsistent begin/end types in range-based 'for' statement: 'FiboIterator' and 'int'
   34 |  for (const auto i : FiboRange{ 5000 }) {
      |                                      ^
main.cpp:34:38: error: conversion from 'int' to non-scalar type 'FiboIterator' requested
main.cpp:34:38: error: no match for 'operator!=' (operand types are 'FiboIterator' and 'FiboIterator')
main.cpp:7:7: note: candidate: 'bool FiboIterator::operator!=(int) const'
    7 |  bool operator!=(int x) const {
      |       ^~~~~~~~
main.cpp:7:22: note:   no known conversion for argument 1 from 'FiboIterator' to 'int'
    7 |  bool operator!=(int x) const {
      |                  ~~~~^

MSVC和G++之间是否存在显著差异? 如果我想使自定义范围工作与g++,我应该如何改变我的代码? 谢了。


共2个答案

匿名用户

我同意@vlad-from-moscow的观点,这可能是一个MSVC bug,因为Visual Studio 2019的默认设置是C++14。 它不应该编译。

自C++17以来,您的代码是正确的。 如果您使用C++17,您的代码将在GCC和clang上编译。

基于范围的for循环的实现已经改变。

C++11:

{
   auto && __range = range_expression ; 
   for (auto __begin = begin_expr, __end = end_expr; 
       __begin != __end; ++__begin) { 
       range_declaration = *__begin; 
       loop_statement 
   }
} 

C++17:

{        
    auto && __range = range_expression ; 
    auto __begin = begin_expr ;
    auto __end = end_expr ;
    for ( ; __begin != __end; ++__begin) { 
        range_declaration = *__begin; 
        loop_statement 
    } 
}

begin迭代器的类型为FiboIterator,end迭代器的类型为int。

// c++11 version fails, auto can't deduce type
auto __begin = begin_expr, __end = end_expr; // an error here

// C++17 version works fine, they are different types.
auto __begin = begin_expr ;
auto __end = end_expr;

如果您不想使用C++17,那么您应该使begin和end的返回类型相同,并使FiboIterator的比较运算符也相同。

#include <cstdio>

class FiboIterator {
    int current{ 1 };
    int last{ 1 };
public:
    FiboIterator(int x=1) : current{x} {}

    bool operator!=(FiboIterator x) const {
        return x.current >= current;
    }
    FiboIterator& operator++() {
        const auto tmp = current;
        current += last;
        last = tmp;
        return *this;
    }
    int operator*() {
        return current;
    }
};

class FiboRange {
    const int max;
public:
    explicit FiboRange(int max): max{max} {}

    FiboIterator begin() const {
        return FiboIterator{};
    }
    FiboIterator end() const {
        return FiboIterator{max};
    }
};

匿名用户

看来是MS VS的一个bug。

特别是基于范围的for循环被转换为如下语句

for ( auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin )

即有使用声明

auto __begin = begin-expr, __end = end-expr;

但是,函数begin和end具有不同的返回类型。

FiboIterator begin() const {
    return FiboIterator{};
}
int end() const {
    return max;
}

因此您不能在这样的声明中使用占位符说明符auto

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|自定义|类型|msvc|编译|g++|编译)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?