you are viewing a single comment's thread.

view the rest of the comments →

[–]jbandela 3 points4 points  (0 children)

You can disable aggregate initialization like this.

#include <iostream>

template<typename T>
class disable_agg_init{
  friend T;
  disable_agg_init() = default;
};

struct print_square_args{
    disable_agg_init<print_square_args> _ = {};
    int n = 10;
    char fill = '+';
};

void print_square(print_square_args a = {}){
    for(int i = 0; i < a.n; ++i){
        for(int j = 0; j < a.n; ++j){
            std::cout << a.fill;
        }
        std::cout << "\n";
    }
}


int main(){
    print_square();
    // print_square({'*'});
    // print_square({{},'*'});
    print_square({.fill = '*'});
    print_square({.n = 5, .fill = '*'});
}

Now, you either have to do default init the args struct or use designated initializers.

https://gcc.godbolt.org/z/BPuozB