Используйте std :: tuple для списка параметров шаблона вместо списка типов
Я пытаюсь вызвать шаблонную функцию следующим образом:
typedef std::tuple InstrumentTuple;
Cache cache;
InstrumentTuple tuple = cache.get();
Я знаю, что могпросто" передать типы кортежа. Это то, что я знаю, но это довольно громоздко, так как я делаю много вызовов этой функции и так как кортежи довольно длинные:
InstrumentTuple tuple = c.get(); // syntax I'd like to avoid
Поэтому я попробовал несколько реализаций метода get, но безуспешно:
Включение через параметр шаблона#include
class Cache
{
private:
template
std::tuple get() // line 6
{
return std::tuple();
}
template
std::tuple get() // line 12
{
std::tuple value;
return std::tuple_cat(value, get());
}
public:
template
T get()
{
return get(); // line 22
}
};
int main(int argc, char** argv)
{
Cache cache;
typedef std::tuple InstrumentTuple;
InstrumentTuple tuple = cache.get(); // line 30
}
Что дает мне эту ошибку:
main.cpp: In instantiation of 'T Cache::get() [with T = std::tuple]':
main.cpp:30:56: required from here
main.cpp:22:26: error: no matching function for call to 'Cache::get()'
main.cpp:22:26: note: candidates are:
main.cpp:6:18: note: template std::tuple Cache::get()
main.cpp:6:18: note: template argument deduction/substitution failed:
main.cpp:5:33: error: no type named 'type' in 'struct std::enable_if'
main.cpp:12:81: note: template std::tuple Cache::get()
// ----- Important part
main.cpp:12:81: note: template argument deduction/substitution failed:
main.cpp: In substitution of 'template std::tuple Cache::get() [with int I = 0; T = std::tuple; = ]':
// -----
main.cpp:22:26: required from 'T Cache::get() [with T = std::tuple]'
main.cpp:30:56: required from here
main.cpp:12:81: error: no matching function for call to 'Cache::get()'
main.cpp:12:81: note: candidate is:
main.cpp:6:18: note: template std::tuple Cache::get()
main.cpp:6:18: note: template argument deduction/substitution failed:
main.cpp:5:33: error: no type named 'type' in 'struct std::enable_if'
main.cpp: In instantiation of 'T Cache::get() [with T = std::tuple]':
main.cpp:30:56: required from here
main.cpp:20:7: note: template T Cache::get()
main.cpp:20:7: note: template argument deduction/substitution failed:
main.cpp:22:26: error: wrong number of template arguments (2, should be 1)
Я нене понимаю, почему отсутствует параметр шаблона.
Поэтому я попробовал другую реализацию:
Шаблон-шаблон с именем параметра#include
class Cache
{
private:
template
std::tuple get() // line 7
{
return std::tuple();
}
template
std::tuple get() // line 13
{
return std::tuple_cat(std::tuple(), get());
}
public:
template
typename std::tuple get()
{
return get();
}
}; // line 24
int main(int argc, char** argv)
{
Cache cache;
typedef std::tuple InstrumentTuple;
InstrumentTuple tuple = cache.get(); // line 30
}
Но тогда я получаю эту ошибку:
// ----- Important part
main.cpp:24:1: error: expected identifier before '}' token
main.cpp:24:1: error: expected unqualified-id before '}' token
// -----
main.cpp: In function 'int main(int, char**)':
main.cpp:30:56: error: no matching function for call to 'Cache::get()'
main.cpp:30:56: note: candidates are:
main.cpp:7:18: note: template std::tuple Cache::get()
main.cpp:7:18: note: template argument deduction/substitution failed:
main.cpp:13:31: note: template std::tuple Cache::get()
main.cpp:13:31: note: template argument deduction/substitution failed:
Опять я нене понимаю ошибки из-за отсутствующего идентификатора.
Теперь мне интересно, возможно ли то, чего я хочу достичь. Можно ли использоватьstd::tuple
как я хочу? Или есть лучший способ?