mercredi 28 décembre 2016

pack expansion with braced-init-list

I decide to implement python's slice with c++ by myself. I wrote a function which accepts variadic slice_info<int> arguments. I compiled with MSVC2015 and it's like the following

template<typename T>
struct slice_info {
    T fr, to, step;
    slice_info(std::initializer_list<T> il) {
    }
    slice_info(const slice_info<T> & x) : fr(x.fr), to(x.to), step(x.step){
    }
};
void slice(slice_info<int>&& s) {
}
template<typename ... Args>
void slice(slice_info<int>&& s, Args&& ... args) {
    slice(std::forward<Args>(args)...);
}
void slice_work_around(const std::vector<slice_info<int>> & vs) {

}
int main(){     
    slice({ 1, 2 }, { 3, 4, 5 }, {7}); // #1 error
    slice({ 1, 2 }, slice_info<int>{ 3, 4, 5 }, slice_info<int>{7}); // #2 yes
    slice_work_around({ {1, 2}, {3, 4, 5}, {7} }); // #3 yes
}

I thought #1 error is because

braced-init-list is not an expression and therefore has no type

I tried #2 and #3 and they worked. However I am still wondering is there are possible ways to make #1 possible. This question is a bit similar with c11-variable-number-of-arguments-same-specific-type, and in my case these variable number of arguments are braced-init-list.

Aucun commentaire:

Enregistrer un commentaire