从' complex float转换' to' int'不允许在转换的常数表达式中

发布于 2025-02-12 00:53:12 字数 1940 浏览 1 评论 0原文

我正在使用Protobuf来交换一些消息,但是当我尝试编译使用消息的代码时,我在重复字段中有此转换错误。H文件,特别是在下面的代码中。 是版本问题吗?

.proto文件

message mymessage {

    repeated double message = 20;
}

Protobuf重复字段。h

  template <int I>
  class FastAdderImpl<I, false> {
   public:
    explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
    void Add(const Element& val) { repeated_field_->Add(val); }

   private:
    RepeatedField* repeated_field_;
    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  };

错误:

/usr/local/include/google/protobuf/repeated_field.h:473:17: error: expected ')'
  template <int I>
                ^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
          ^
/usr/include/complex.h:48:21: note: expanded from macro '_Complex_I'
#define _Complex_I      (__extension__ 1.0iF)
                         ^
/usr/local/include/google/protobuf/repeated_field.h:473:17: note: to match this '('
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
          ^
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I      (__extension__ 1.0iF)
                        ^


 error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
  class FastAdderImpl<I, false> {
                      ^


/usr/local/include/google/protobuf/repeated_field.h:474:23: error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
  class FastAdderImpl<I, false> {
                      ^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
          ^~~~~~~~~~
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I      (__extension__ 1.0iF)
                        ^~~~~~~~~~~~~~~~~~~~~
2 errors generated.

I am using protobuf to exchange some messages but when i try to compile the code that uses the messages i am having this conversion error in the repeated field.h file, specifically in the code below.
Is it a version problem ?

.proto file

message mymessage {

    repeated double message = 20;
}

protobuf repeated field.h

  template <int I>
  class FastAdderImpl<I, false> {
   public:
    explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
    void Add(const Element& val) { repeated_field_->Add(val); }

   private:
    RepeatedField* repeated_field_;
    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  };

Error:

/usr/local/include/google/protobuf/repeated_field.h:473:17: error: expected ')'
  template <int I>
                ^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
          ^
/usr/include/complex.h:48:21: note: expanded from macro '_Complex_I'
#define _Complex_I      (__extension__ 1.0iF)
                         ^
/usr/local/include/google/protobuf/repeated_field.h:473:17: note: to match this '('
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
          ^
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I      (__extension__ 1.0iF)
                        ^


 error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
  class FastAdderImpl<I, false> {
                      ^


/usr/local/include/google/protobuf/repeated_field.h:474:23: error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
  class FastAdderImpl<I, false> {
                      ^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
          ^~~~~~~~~~
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I      (__extension__ 1.0iF)
                        ^~~~~~~~~~~~~~~~~~~~~
2 errors generated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2025-02-19 00:53:12

问题是,非类型模板参数必须为编译时间常数,但是非型模板参数的类型(int )在您的示例中,与传递参数的类型不同((__ Extension__ 1.0if)),因此需要转换。

也许一个人为的例子可能会进一步解决:

constexpr float k = 4.4f;
template<int I> 
void f()
{

}
int main()
{
//----v----->not valid and will produce a similar error:conversion from 'float' to 'int' in a converted constant expression
    f<k>();
}

以上会产生类似的错误,说:

conversion from 'float' to 'int' in a converted constant expression
could not convert 'k' from 'const float' to 'int'

demo

The problem is that a non-type template argument must be a compile-time constant but the type of the non-type template parameter(int) in your example is not the same as the type of the passed argument((__extension__ 1.0iF)) and so requires a conversion.

Perhaps a contrived example might clear this up further:

constexpr float k = 4.4f;
template<int I> 
void f()
{

}
int main()
{
//----v----->not valid and will produce a similar error:conversion from 'float' to 'int' in a converted constant expression
    f<k>();
}

The above will produce a similar error saying:

conversion from 'float' to 'int' in a converted constant expression
could not convert 'k' from 'const float' to 'int'

Demo

执手闯天涯 2025-02-19 00:53:12

问题可能是由于某些原因,您将包括 &lt; complect。 h&gt; c标题。 (或 &lt; ccomplex&gt;

此标头不应包含在C ++中,但是如果您这样做,则 <代码> i 将是宏。在GCC中,将其定义为_complex_i,又定义为(__ Extension__ 1.0if);无论哪种方式,很明显它是无效的语法。

修复程序是使用与i不同的变量名称。

(轶事:此错误是在flint中遇到的,最近修复了。)

The problem is likely that for some reason you're including the <complex.h> C header. (or <ccomplex> which is deprecated)

This header shouldn't be included in C++, but if you do, then I would be a macro. In gcc, it would be defined as _Complex_I which is in turn defined as (__extension__ 1.0iF); either way, it is quite clear that it is invalid syntax.

The fix is to use a variable name different from I.

(anecdote: this bug is encountered in flint, recently fixed.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文