I would like to do something like this:
template <typename T, int Size>
struct config {
T field;
/* various other things */
}
template <typename C> // where C is a specific version of the 'config' object.
void func(C arg0, C::T arg1, std::array<int, C::Size> &arg2) {
int i = C::Size;
}
ie, can I choose the types my function parameters based on the full type of config
?
I could achieve this by just listing out all the individual types:
template <typename T, int Size>
void func(config<T,Size> config, T arg1, std::array<int, Size> &arg2) { ... }
But my full use case is acquiring quite a large number of parameters, both compile-time and run-time, and I'd like to package them up into my config object and pass it around, without having to replicate many template definitions at dozens of function declarations. Is there a way to do this?
You can of course add a type alias/static constexpr to config
, but the only way to guarantee the first parameter of the function is instantiation of config
, is the second alternative.
A solution using type alias/ static const:
template <class T, int N>
struct config
{
using FieldType = T;
static constexpr int Size = N;
FieldType field;
};
template <typename C>
void func(C arg0, typename C::FieldType arg1, std::array<int, C::Size> &arg2) {
int i = C::Size;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.