Chinaunix

标题: 这个也不是c++语法吧?? [打印本页]

作者: yjh777    时间: 2016-04-15 16:22
标题: 这个也不是c++语法吧??
本帖最后由 yjh777 于 2016-04-15 16:23 编辑

Internet-Draft
    https://tools.ietf.org/wg/nfsv4/ ... -from-06.wdiff.html

里面有这样的代码,上次来问了一遍,有人说是c++语法;可是最近翻 c++ primer 也没看到过这样的用法:

   <CODE BEGINS>

   /// struct ff_device_versions4 {
   ///         uint32_t        ffdv_version;
   ///         uint32_t        ffdv_minorversion;
   ///         uint32_t        ffdv_rsize;
   ///         uint32_t        ffdv_wsize;
   ///         bool            ffdv_tightly_coupled;
   /// };
   ///

   /// struct ff_device_addr4 {
   ///         multipath_list4     ffda_netaddrs;
   ///         ff_device_versions4 ffda_versions<>;      //<<<------------------- 这里
   /// };
   ///

   <CODE ENDS>

尝试编译报错:      ff_device_versions4 ffda_versions<>;   这个写法真的是 C++ 语法吗?还是伪代码?
  1. $ cat kkk.cpp
  2. #include <vector>
  3. #include <cstdint>

  4. using std::vector;

  5. struct ff_device_versions4 {
  6.         uint32_t        ffdv_version;
  7.         uint32_t        ffdv_minorversion;
  8.         uint32_t        ffdv_rsize;
  9.         uint32_t        ffdv_wsize;
  10.         bool            ffdv_tightly_coupled;
  11. };

  12. struct ff_device_addr4 {
  13.         int ffda_netaddrs;
  14.         ff_device_versions4 ffda_versions<>;
  15. };
  16. $ g++ -std=c++11 kkk.cpp
  17. kkk.cpp:16:22: 错误:expected ‘;’ at end of member declaration
  18.   ff_device_versions4 ffda_versions<>;
  19.                       ^
  20. kkk.cpp:16:35: 错误:expected unqualified-id before ‘<’ token
  21.   ff_device_versions4 ffda_versions<>;
  22.                                    ^

复制代码

作者: yjh777    时间: 2016-04-15 17:18
知道了,不是什么 C++ 语法; 是 rpc .x 的特有语法,定义变长数组的:

The  rpcgen  compiler transfers this declaration into
the header file without modification.

You can also specify variable length arrays.

The maximum size of the array is enclosed in angle braces.  You can
omit the size to indicate that there is no maximum value.
For example, consider the following declaration:

        long x_coords<50>;

This declares  x_coords  to be an array of
0 to 50 long integers.  Similarly, the following declaration:

        long z_coords<>;


https://cs.nyu.edu/courses/spring00/V22.0480-002/class07.html
作者: yjh777    时间: 2016-04-15 17:21
更详细的介绍:  http://neo.dmcs.pl/rso/du/onc-rpc3.html

The syntax for each, followed by examples, is listed here:

    Simple declarations

            simple-declaration:
                    type-ident variable-ident

    For example, colortype color in XDR, is the same in C: colortype color. [Return to example]

    Fixed-length array declarations

            fixed-array-declaration:
                    type-ident variable-ident "[" value "]"

    For example, colortype palette[8] in XDR, is the same in C: colortype palette[8]. [Return to example]

    Variable-length array declarations

    These have no explicit syntax in C, so XDR creates its own by using angle brackets, as in the following syntax:

    variable-array-declaration:
            type-ident variable-ident "<" value ">"
            type-ident variable-ident "<" ">"

    The maximum size is specified between the angle brackets; it may be omitted, indicating that the array can be of any size, as shown in the following example:

            int heights<12>;        /* at most 12 items */
            int widths<>;           /* any number of items */

    Variable-length arrays have no explicit syntax in C, so each of their declarations is compiled into a struct. For example, the heights declaration is compiled into the following struct:

        struct {
            u_int heights_len; /* number of items in array */
            int *heights_val;  /* pointer to array */
        } heights;

    Here, the _len component stores the number of items in the array and the _val component stores the pointer to the array. The first part of each of these component names is the same as the name of the declared XDR variable. [Return to example]

    Pointer declarations

    These are the same in XDR as in C. You cannot send pointers over the network, but you can use XDR pointers to send recursive data types, such as lists and trees. In XDR language, this type is called optional-data, not pointer, as in the following syntax:

    optional-data:
            type-ident "*"variable-ident

    An example of this (the same in both XDR and C) follows:

            listitem *next;






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2