【问题标题】:C++ Passing a reference to an anonymous structure - can you?C++ 传递对匿名结构的引用 - 你可以吗?
【发布时间】:2017-08-07 08:32:51
【问题描述】:

使用如下所示的结构,我可以像这样使用它。

NMEADecodedSentence s;
auto & gga = s.GGA;
auto alt = gga.Alt;

但是是否可以将对匿名结构的引用传递给函数,例如:

SomeFunc(gga);

如果是这样,函数签名会是什么样子?我的想法是不,你不能不命名结构,但我只是想知道是否有一些我不知道的聪明方法!

struct NMEADecodedSentence
    {
        GNSSSentenceType Type;
        GNSSTalkerId TalkerId;
        union
        {
            struct
            {
                char Time[10];          // UTC time - hhmmss.ss
                char Lat[13];           // Latitude (degrees & minutes) - ddmm.mmmmm
                char NS;                // North/South indicator
                char Long[14];          // Longitude (degrees & minutes) - dddmm.mmmmm
                char EW;                // East/West indicator
                uint8_t Quality;        // Quality indicator for position fix
                uint8_t NumSV;          // Number of satellites used (0-12)
                float HDOP;             // Horizontal Dilution of Precision
                float Alt;              // Altitude above mean sea level - meters
            }GGA;
            struct // Recommended minimum data
            {
                char Time[10];          // UTC time - hhmmss.ss
                char Status;            // Status, V = Navigation receiver warning, A = Data valid
                char Lat[13];           // Latitude (degrees & minutes) - ddmm.mmmmm
                char NS;                // North/South indicator
                char Long[14];          // Longitude (degrees & minutes) - dddmm.mmmmm
                char EW;                // East/West indicator
                float Spd;              // Speed over ground - knots
                float COG;              // Course over ground - degrees
                char Date[7];           // UTC Date - ddmmyy
            }RMC;
            struct // Course over ground and ground speed
            {
                float COGT;             // Course over ground (true) - degrees
                float COGM;             // Course over ground (magnetic) - degrees
                float Kph;              // Speed over ground - kph
            }VTG;
        };
    };

【问题讨论】:

  • 你想在函数声明中使用auto吗?
  • 转述:您可以这样做,但应该吗?在声明中省略名称真的值得在接收函数中使用额外的样板吗?你真的从中得到了什么?
  • @underscore_d 声明可能是某种库或供应商提供的代码,因此他可能不会冒险更改它(编辑具有外部源的标头总是坏主意)

标签: c++ structure anonymous


【解决方案1】:

如果编译器支持 decltype 功能,您可以这样做(标准的一部分,但我们都知道它是怎么回事..)

char foo2(decltype (NMEADecodedSentence::GGA) & param)
{
     return param.EW; 
}

decltype 对于模板构建非常有用。在这种情况下,NMEADecodedSentence::GGA 作为类的非静态成员的名称是可用的。

开放模板可用,但类型不安全,因为您可能会尝试提供包含函数中使用的相同字段而不是 NMEADecodedSentence::GGA 的任何内容。这可能是架构的特性或错误特性。

你可以这样保护模板:

#include <type_traits>

template<typename T>
char foo( T &a )
{
  static_assert(std::is_same<T,decltype (NMEADecodedSentence::GGA)>::value || 
                std::is_same<T,decltype (NMEADecodedSentence::RMC)>::value, 
                "Wrong type of foo() argument" );
  return a.EW;  
}

static_assert 会产生编译时错误(这就是为什么它是“静态”的),如果您提供 foo 与不同类型的参数:std::is_same&lt;&gt;::value 将是错误的。

请注意,您也可以这样做:

struct  GGAType : public decltype (NMEADecodedSentence::GGA)
{
};

但是如果没有修复 static_assert 表达式,上述模板将不接受这种类型的参数。 foo2() 可以工作。

【讨论】:

    【解决方案2】:

    这应该可行:

    void f(decltype(NMEADecodedSentence::GGA)& gga)
    {
        ...
    }
    

    【讨论】:

    • 是的,确实确实回答了我的问题。好想法!谢谢...
    猜你喜欢
    • 2020-09-23
    • 2016-07-23
    • 2016-07-02
    • 2013-04-18
    • 2020-10-28
    • 2020-10-20
    • 2020-08-19
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多