C++ 多参数模板化 class 成员特化
c++ multiple parameters templated class member specialization
我正在设置一个 c++(11) 程序,我在其中使用依赖于 2 个参数的模板化 class。 class 的大部分内容都可以根据模板参数进行通用编写。只有少数功能需要专门的版本。这是重现我的问题的示例模式:
template<class T, int N>
class foo
{
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab);
};
// ...
template<class T, int N>
foo<T,N>::myfunc(myarray tab)
{
// generic version
}
// need specialization only of myfunc:
template<class T>
foo<T,1>::myfunc(myarray tab)
{
// specialized version for N=1
}
然后编译器抱怨:error: invalid use of incomplete type ‘class foo<T, 1>’
关于第 template<class T> foo<T,1>::myfunc(myarray tab)
行
我发现唯一可行的解决方法是插入 class 的完整副本及其专用版本:
template<class T>
class foo<T,1>
{
// recopy all the lines of class foo<T,N>, replacing N by 1
};
// duplicate as well all generic function definition with
// specialized versions <T,1> even when not needed
很不满意的地方...
经过一些实验,我发现当模板只使用1个参数时(例如template <int N> class foo{...};
)似乎不会出现这个问题,但至少涉及2个参数时才会出现。
这是 C++ 编程中众所周知的东西吗?有没有更聪明的方法来解决我的问题? (我想创建一个没有专门功能的母亲 class,然后让 class foo 继承它,只保留专门的成员,以最小化 "duplication workaround")
感谢指教!
简答:您可以用这种方式完全特化成员,但不能部分特化它们。
长答案:
当您的 class 只有一个模板参数时,例如:
template <class T> struct X {
void foo() { };
}
template <> void X<int>::foo() { }
是全特化,是允许的。然而,
template <class T, class Y> struct X {
void foo() { };
}
template <class T> void X<int, Y>::foo() { }
是偏特化,不允许对单个成员进行偏特化——需要对整个class.
进行偏特化
谷歌搜索 Specialization of templated member function in templated class 并采用 Matthieu M. 的答案,我想我找到了解决我问题的方法(重载):
template<int N> class virtualclass{};
template<class T, int N>
class foo
{
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab)
{
helperfunc((virtualclass<N>*)0, tab);
}
template<class P>
void helperfunc(P*, myarray tab);
void helperfunc(virtualclass<1>*, myarray tab);
};
// ...
template<class T, int N>
template<class P>
foo<T,N>::helperfunc(P*,myarray tab)
{
// generic version
}
// need specialization only of myfunc:
template<class T, int N>
foo<T,N>::helperfunc(virtualclass<1>*, myarray tab)
{
// specialized version for N=1
}
这看起来是个好习惯吗?这个解决方案可能有一些隐藏的缺点吗?
标签派遣救援。
使用 std::integral_constant
,我们可以创建两种类型,一种用于通用 N
,另一种用于 1
(或者您可以在 [=16] 上定义一些其他类型模板=],但我选择使用已经存在的东西)。
void myfunc(myarray tab)
{
myfunchelper(tab,
typename std::is_same<std::integral_constant<int, N>,
std::integral_constant<int, 1>>::type{});
}
将调用分派到为 std::true_type
(N==1
的场景)和 std::false_type
(N != 1
的场景)重载的辅助函数:
void myfunchelper(myarray tab, std::false_type);
void myfunchelper(myarray tab, std::true_type);
请注意,我故意不命名该类型,因为它未被使用,并且智能编译器会优化掉该类型的任何类型的分配(我认为)。
Live Demo
(来自下面演示的代码):
#include <array>
#include <iostream>
#include <type_traits>
template<class T, int N>
class foo
{
public:
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab)
{
myfunchelper(tab, typename std::is_same<std::integral_constant<int, N>, std::integral_constant<int, 1>>::type{});
}
private:
void myfunchelper(myarray tab, std::false_type)
{
std::cout << "Generic myfunc\n";
}
void myfunchelper(myarray tab, std::true_type)
{
std::cout << "myfunc specialized for N==1\n";
}
};
int main()
{
std::array<char, 1> arr1{{'c'}};
std::array<double, 2> arr2{{1.0, 2.0}};
foo<char, 1> f1;
foo<double, 2> f2;
f1.myfunc(arr1); // calls specialized version
f2.myfunc(arr2); // calls generic version
}
关于您发布的解决方案,这也是一种标签分发方法,它涉及定义另一个 int
-template class,它在周围范围中引入了不必要的类型,并且还涉及转换 a 和 NULL
(好吧,在这种情况下为 0,这就是 NULL
通常的类型定义)。
实际上,我发布的解决方案是相同的,但我认为它更清晰一些,类型更安全一些。
我正在设置一个 c++(11) 程序,我在其中使用依赖于 2 个参数的模板化 class。 class 的大部分内容都可以根据模板参数进行通用编写。只有少数功能需要专门的版本。这是重现我的问题的示例模式:
template<class T, int N>
class foo
{
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab);
};
// ...
template<class T, int N>
foo<T,N>::myfunc(myarray tab)
{
// generic version
}
// need specialization only of myfunc:
template<class T>
foo<T,1>::myfunc(myarray tab)
{
// specialized version for N=1
}
然后编译器抱怨:error: invalid use of incomplete type ‘class foo<T, 1>’
关于第 template<class T> foo<T,1>::myfunc(myarray tab)
我发现唯一可行的解决方法是插入 class 的完整副本及其专用版本:
template<class T>
class foo<T,1>
{
// recopy all the lines of class foo<T,N>, replacing N by 1
};
// duplicate as well all generic function definition with
// specialized versions <T,1> even when not needed
很不满意的地方...
经过一些实验,我发现当模板只使用1个参数时(例如template <int N> class foo{...};
)似乎不会出现这个问题,但至少涉及2个参数时才会出现。
这是 C++ 编程中众所周知的东西吗?有没有更聪明的方法来解决我的问题? (我想创建一个没有专门功能的母亲 class,然后让 class foo 继承它,只保留专门的成员,以最小化 "duplication workaround")
感谢指教!
简答:您可以用这种方式完全特化成员,但不能部分特化它们。
长答案: 当您的 class 只有一个模板参数时,例如:
template <class T> struct X {
void foo() { };
}
template <> void X<int>::foo() { }
是全特化,是允许的。然而,
template <class T, class Y> struct X {
void foo() { };
}
template <class T> void X<int, Y>::foo() { }
是偏特化,不允许对单个成员进行偏特化——需要对整个class.
进行偏特化谷歌搜索 Specialization of templated member function in templated class 并采用 Matthieu M. 的答案,我想我找到了解决我问题的方法(重载):
template<int N> class virtualclass{};
template<class T, int N>
class foo
{
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab)
{
helperfunc((virtualclass<N>*)0, tab);
}
template<class P>
void helperfunc(P*, myarray tab);
void helperfunc(virtualclass<1>*, myarray tab);
};
// ...
template<class T, int N>
template<class P>
foo<T,N>::helperfunc(P*,myarray tab)
{
// generic version
}
// need specialization only of myfunc:
template<class T, int N>
foo<T,N>::helperfunc(virtualclass<1>*, myarray tab)
{
// specialized version for N=1
}
这看起来是个好习惯吗?这个解决方案可能有一些隐藏的缺点吗?
标签派遣救援。
使用 std::integral_constant
,我们可以创建两种类型,一种用于通用 N
,另一种用于 1
(或者您可以在 [=16] 上定义一些其他类型模板=],但我选择使用已经存在的东西)。
void myfunc(myarray tab)
{
myfunchelper(tab,
typename std::is_same<std::integral_constant<int, N>,
std::integral_constant<int, 1>>::type{});
}
将调用分派到为 std::true_type
(N==1
的场景)和 std::false_type
(N != 1
的场景)重载的辅助函数:
void myfunchelper(myarray tab, std::false_type);
void myfunchelper(myarray tab, std::true_type);
请注意,我故意不命名该类型,因为它未被使用,并且智能编译器会优化掉该类型的任何类型的分配(我认为)。
Live Demo
(来自下面演示的代码):
#include <array>
#include <iostream>
#include <type_traits>
template<class T, int N>
class foo
{
public:
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab)
{
myfunchelper(tab, typename std::is_same<std::integral_constant<int, N>, std::integral_constant<int, 1>>::type{});
}
private:
void myfunchelper(myarray tab, std::false_type)
{
std::cout << "Generic myfunc\n";
}
void myfunchelper(myarray tab, std::true_type)
{
std::cout << "myfunc specialized for N==1\n";
}
};
int main()
{
std::array<char, 1> arr1{{'c'}};
std::array<double, 2> arr2{{1.0, 2.0}};
foo<char, 1> f1;
foo<double, 2> f2;
f1.myfunc(arr1); // calls specialized version
f2.myfunc(arr2); // calls generic version
}
关于您发布的解决方案,这也是一种标签分发方法,它涉及定义另一个 int
-template class,它在周围范围中引入了不必要的类型,并且还涉及转换 a 和 NULL
(好吧,在这种情况下为 0,这就是 NULL
通常的类型定义)。
实际上,我发布的解决方案是相同的,但我认为它更清晰一些,类型更安全一些。