c++cuda

Inline struct initialization, "nonstatic member must be relative to a static object"


I have a small issue with referencing the outer struct members from a nested struct. When I try to set x and y to width and height it shows the error "a nonstatic member must be relative to a static object", here is the code:

const int width = 1280, height = 720;
struct MandelbrotBase
{
    int width = ::width, height = ::height;
    ...
    struct
    {
        int x = width / 2;
        int y = height / 2;
    } pos;
} mb;
__global__ void generate_mandelbrot(unsigned int* colors, MandelbrotBase mb)
{
...
}

Is not that a big problem, since I can reference the global ::width and ::heigth instead, or other methods to initialize. But, is there any possibility to do do it just inline? Using a member constructor or function is not an option, at least is not a preferred option, because I use it to pass information to cuda kernel functions, so I want to keep it very simple.


Solution

  • You can make it like

    const int width = 1280, height = 720;
    
    struct MandelbrotBase {
      int width = ::width, height = ::height;
      struct {
        int x;
        int y;
      } pos = {.x = width / 2, .y = height / 2};
    } mb;
    
    void generate_mandelbrot(unsigned int* colors, MandelbrotBase mb) {}
    

    Non static width and height can't be referenced outside MandelbrotBase members without an object. The unnamed struct is not a part of the struct MandelbrotBase definition, the unnamed struct is just defined in the struct MandelbrotBase name scope.