具有char []字段的POD结构的constexpr构造 [英] constexpr construction of a POD struct with a char[] field

查看:66
本文介绍了具有char []字段的POD结构的constexpr构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 constexpr 下创建函数?

这是我用来创建在"C"目录中定义的POD结构的函数.我无法更改的头文件.

This is a function that I use to create a POD struct that is defined in a "C" header file that I cannot change.

我最终得到了下面的静态函数助手(可以工作)来制作这些结构,我相信它不是 constexpr ,因为 strncpy 调用违反了constexpr .

I ended up with the following static function helper (which works) to make these structs, I believe it is not constexpr, as the strncpy call violates constexpr.

static auto makeWPTEntry(
    const char* name,
    double aLatDeg, double aLatMin, double aLonDeg,
    double aLonMin, char bSeven = false,
    double aCourse = 0, double aAzimuth = 0, double aDist = 0,
    int aETASecs = 0, int aMod = 0)->WPTEntry {
    auto result = WPTEntry{
        bSeven,
        {},
        (aLatDeg + aLatMin) / 60.0,
        (aLonDeg + aLonMin) / 60.0,
        aCourse, aAzimuth,
        aDist, aETASecs, aMod
    };
    strncpy(result.name, name, sizeof(result.name));
    return result;
}

我很难按照以下几行做一些事情(即 constexpr )-但是,我确实需要带有 const char *的非 constexpr 函数签名name 参数,不幸的是,我不知道如何将该 const char * 内联转换为固定大小的数组 char const(& name)[SIZ_WAYPOINT_NAME] 与struct字段的构造函数兼容.

I an struggling to make something along the following lines (which is constexpr) - however I really need the non constexpr function signature with a const char* name parameter, Unfortunately, I don't know how to inline convert this const char* into a fixed sized array char const (&name)[SIZ_WAYPOINT_NAME] to be compatible with the struct field's constructor.

constexpr auto makeWPTEntry(
    char const (&name)[SIZ_WAYPOINT_NAME],
    double aLatDeg, double aLatMin, double aLonDeg,
    double aLonMin, char bSeven = false,
    double aCourse = 0, double aAzimuth = 0,
    double aDist = 0,
    int aETASecs = 0, int aMod = 0)->WPTEntry {
    auto result = WPTEntry{
        bSeven,
        name,
        (aLatDeg + aLatMin) / 60.0,
        (aLonDeg + aLonMin) / 60.0,
        aCourse, aAzimuth,
        aDist, aETASecs, aMod
    };
    //strncpy(result.name, name, sizeof(result.name));
    return result;
}

WPTEntry 是一个简单的外部C数据结构.

The WPTEntry is a simple external C data structure.

#define SIZ_WAYPOINT_NAME 9

typedef struct {
    char    seven;
    char    name[SIZ_WAYPOINT_NAME];
    double  lat;        
    double  lon;        
    double  crs;        
    double  az2;        
    double  distance;   
    int     eta_secs;  
    int     mod;
} WPTEntry;

我创建这些结构的方式如下:

The way I create these structures is as follows:

const auto wpt = makeWPTEntry("", -24.499, 0, -81.501, 0);

我问了类似的问题问题以前,但是我从未收到答案.

I asked something similar question before, however I never received an answer.

我从此网站中找到了以下内容,但我不确定如何处理能够使用它来适应我的参数,也许它可能有用.

I found the following from this site but I am not sure how I might be able to use it to adapt my parameter, perhaps its might be useful of not.

struct ArrayWrapper
{
   char const *address_;
   size_t      length_;

   template<int N>
   ArrayWrapper(char const (&array)[N])
   {
      address = &array[0];
      length_ = N;
   }
};

推荐答案

但实际上,要成为一个男人并复制它:

But really, be a man and copy it:

template<std::size_t N>
constexpr WPTEntry makeWPTEntry(
            char const (&name)[N],
            double aLatDeg, double aLatMin, double aLonDeg,
            double aLonMin, char bSeven = false,
            double aCourse = 0, double aAzimuth = 0,
            double aDist = 0,
            int aETASecs = 0, int aMod = 0) {
    auto r = WPTEntry{
        bSeven,
        { 0 },
        (aLatDeg + aLatMin) / 60.0,
        (aLonDeg + aLonMin) / 60.0,
        aCourse, aAzimuth,
        aDist, aETASecs, aMod
    };
    for (size_t i = 0; i < N; ++i) {
        r.name[i] = name[i];
    }
    return r;
}

int main() {
    constexpr auto wpt = makeWPTEntry("abc", -24.499, 0, -81.501, 0);
    std::cout << wpt.name;
}

这篇关于具有char []字段的POD结构的constexpr构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆