1

This question is mostly about the design approach, and I would like to know how to solve such kind of problems in the modern C++ language.

I have a library function that defined like (this is a real code from the compiler):

template <info::device param>
typename info::param_traits<info::device, param>::return_type
get_info() const;

In order to call this function, I could write something like:

some_device.get_info<cl::sycl::info::device::device_type>()

where cl::sycl::info::device::device_type is an actual parameter.

There a long list of supported parameters and I would like to have a collection of result values (results of different function calls).

At this moment, I could do something like:

some_device.get_info<cl::sycl::info::device::param1>()
some_device.get_info<cl::sycl::info::device::param2>()
...
some_device.get_info<cl::sycl::info::device::paramN>()

but because this is terrible, I am looking for a better solution in C++ 11/14.

syscreat
  • 553
  • 1
  • 7
  • 16

2 Answers2

1

With fold expressions no explicit loop (or recursion) is needed. For example:

#include <iostream>
#include <string>

template <typename T>
void foo(){ std::cout << T{}; }   // just an example

template <typename...Args>
void bar() {
    (foo<Args>(),...);            // call foo for each type in Args
}

int main() {
    bar<int,double,std::string>();
}

To have a "collection" of supported types you could use using collection = std::tuple<int,double,std::string>;.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

For all this kind of code I use Boost.Hana iterating on tuples with a boost::hana::for_each, either from user point-of-view but also for SYCL internal implementation.