在转换后的常量表达式中不允许从"_Complex浮点数"转换为"int"



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

。原型文件

message mymessage {
repeated double message = 20;
}

protobuf repeat 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);
};

错误:

/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.

问题非类型模板实参必须是编译时常数但实模板参数的类型(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'

演示

最新更新