C++ 第二个参数的部分模板专门化
我正在围绕一个C api开发一个C++11包装器。C api为各种类型提供了一系列getter,每种类型都有不同的名称。值由给定大小的数组检索,在编译时已知 我想通过模板给出类型和数组大小,以调用正确的函数C++ 第二个参数的部分模板专门化,c++,c++11,templates,specialization,C++,C++11,Templates,Specialization,我正在围绕一个C api开发一个C++11包装器。C api为各种类型提供了一系列getter,每种类型都有不同的名称。值由给定大小的数组检索,在编译时已知 我想通过模板给出类型和数组大小,以调用正确的函数 #include <string> #include <iostream> template <typename T> struct make_stop { constexpr static bool value = false; }; cla
#include <string>
#include <iostream>
template <typename T>
struct make_stop {
constexpr static bool value = false;
};
class Foo
{
public:
Foo() : i(42) {}
template<typename T, size_t n>
T get();
private:
int i = 0;
};
template<typename T, size_t n>
T Foo::get() { static_assert(make_stop<T>::value); return T(); }
template<int, size_t n>
int Foo::get() { return i + n; }
int main() {
Foo foo;
int i = foo.get<int, 4>();
double f = foo.get<double, 2>();
return 0;
}
#包括
#包括
样板
结构使停止{
constexpr静态布尔值=false;
};
福班
{
公众:
Foo():i(42){}
样板
T get();
私人:
int i=0;
};
样板
T Foo::get(){static_assert(make_stop::value);返回T();}
样板
int Foo::get(){return i+n;}
int main(){
富富,;
int i=foo.get();
double f=foo.get();
返回0;
}
但它与正确的功能不匹配
main.cpp:26:5: error: no declaration matches 'int Foo::get()'
int Foo::get() { return i + n; }
^~~
main.cpp:15:7: note: candidate is: 'template<class T, long unsigned int n> T Foo::get()'
T get();
main.cpp:26:5:错误:没有与“int Foo::get()”匹配的声明
int Foo::get(){return i+n;}
^~~
main.cpp:15:7:注意:候选项是:'template T Foo::get()'
T get();
如果我理解正确,您希望部分
专门化T
的get。不幸的是,标准不允许对方法进行部分专门化。但是,您可以通过对T
模板化的类使用静态方法并对该类进行专门化来解决这个问题
像这样:
template <class T> struct Foo_helper;
struct Foo
{
Foo() : i{42} {}
template<class T, std::size_t N>
T get()
{
return Foo_helper<T>::template get<N>(*this);
}
int i = 0;
};
template <class T> struct Foo_helper {};
// specialize Foo_helper for each type T you wish to support:
template <> struct Foo_helper<int>
{
template <std::size_t N>
static int get(const Foo& foo) { return foo.i + N; }
};
template <> struct Foo_helper<double>
{
template <std::size_t N>
static double get(const Foo& foo) { return foo.i + N; }
};
int main()
{
Foo foo{};
int i = foo.get<int, 4>();
double d = foo.get<double, 2>();
}
template-struct-Foo\u-helper;
结构Foo
{
Foo():i{42}{}
样板
不明白
{
返回Foo_helper::template get(*this);
}
int i=0;
};
模板结构Foo_helper{};
//为您希望支持的每种类型指定Foo_助手:
模板结构Foo\u助手
{
样板
静态int-get(const-Foo&Foo){return-Foo.i+N;}
};
模板结构Foo\u助手
{
样板
静态双get(constfoo&Foo){returnfoo.i+N;}
};
int main()
{
富富{};
int i=foo.get();
double d=foo.get();
}
你的问题有点夸张,但是假设你想索引到一些c数组中,并在I
返回值,你不能像你想的那样专门化函数模板,但是你可以使用一些标记,比如
class Foo
{
public:
Foo() : is{1,2,3,4,5,6,7,8,9,10},ds{1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1} {}
template <typename T> struct type_c{};
template <size_t I> struct int_c{};
template<typename T,size_t I>
auto get()
{ return get_impl(type_c<T>(),int_c<I>()); }
private:
template <size_t I>
auto get_impl(type_c<int>,int_c<I>)
{ return is[I]; }
template <size_t I>
auto get_impl(type_c<double>,int_c<I>)
{ return ds[I]; }
int is[10];
double ds[10];
};
int main() {
Foo foo;
int i = foo.get<int,0>();
double d = foo.get<double,2>();
std::cout << i << " " << d << std::endl;
return 0;
}
class-Foo
{
公众:
Foo():is{1,2,3,4,5,6,7,8,9,10},ds{1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1}{
模板结构类型_c{};
模板结构int_c{};
样板
自动获取()
{return get_impl(type_c(),int_c());}
私人:
样板
自动获取\u impl(类型\u c,int\u c)
{返回值为[I];}
样板
自动获取\u impl(类型\u c,int\u c)
{返回ds[I];}
int为[10];
双ds[10];
};
int main(){
富富,;
int i=foo.get();
double d=foo.get();
std::couttemplate
或template
?我的意思是:你声明template
,第一个是数字,第二个是类型,但是你调用(foo.get()
)第一个是类型,第二个是数字。很抱歉,在我的问题中,我在复制粘贴中丢失了,我会编辑代码事实上,顺序并不重要,只要它能工作我不明白。你的意思是foo.get()
,foo.get()
,foo.get()
都应该做同样的事情吗?举一个例子说明foo.get()
和foo.get()
应该执行并返回。您的解释和代码让我非常困惑。另外1:标记分派是干净的,并且将问题转移到重载