是否有某种方法可以指定功能模板参数是否是特定的模板类?

发布于 2025-02-10 03:09:32 字数 1015 浏览 0 评论 0 原文

我将根据以下示例解释我的问题:

template <typename Param1, typename Param2>
class foo1
{
  void lol();
};

template <typename Param1, typename Param2>
class foo2
{
  void lol();
};

////////////////////////// FIRST OPTION //////////////////////////

template <typename Param1, typename Param2>
void func(foo1<Param1, Param2> a)
{
  a.lol();
  a.lol();
}

template <typename Param1, typename Param2>
void func(foo2<Param1, Param2> a)
{
  a.lol();
}

////////////////////////// SECOND OPTION //////////////////////////

template <typename Foo_>
void func(Foo_ a)
{
  a.lol();
  a.lol();
}

///////////////////////////////////////////////////////////////////

int main()
{
  return 0;
}

我的目标是为 func - foo1 foo2 编写两个过载。 第一个选项工作正常,但是我不喜欢我编写未使用的 func 的模板参数。 有什么方法可以避免在 func 的签名中编写模板参数?

我想到写类似的内容第二个选项,但是问题是超载有所不同。

I will explain my question based on following example:

template <typename Param1, typename Param2>
class foo1
{
  void lol();
};

template <typename Param1, typename Param2>
class foo2
{
  void lol();
};

////////////////////////// FIRST OPTION //////////////////////////

template <typename Param1, typename Param2>
void func(foo1<Param1, Param2> a)
{
  a.lol();
  a.lol();
}

template <typename Param1, typename Param2>
void func(foo2<Param1, Param2> a)
{
  a.lol();
}

////////////////////////// SECOND OPTION //////////////////////////

template <typename Foo_>
void func(Foo_ a)
{
  a.lol();
  a.lol();
}

///////////////////////////////////////////////////////////////////

int main()
{
  return 0;
}

My goal is to write two overloads of func - for foo1 and foo2.
The FIRST OPTION works fine, but I don't like that I have to write template parameters for func that are not used in it.
Is there some way to avoid writing template parameters in signature of func?

I thought of writing something like this SECOND OPTION, but the problem is that overloads do different things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2025-02-17 03:09:32

使用模板参数使用使用。没有它们, foo1 foo2 只是类模板而不是类。

最小化打字的一种简单方法是使用模板参数包:

template<class... T>
void func(foo1<T...> a) {
    a.lol();
    a.lol();
}

template<class... T>
void func(foo2<T...> a) {
    a.lol();
}

如果需要此检查很多,则可以创建概念 s(自C ++ 20):

#include <type_traits>

// concept for foo1:
template <class T> struct is_foo1 : std::false_type {};
template <class P1, class P2> struct is_foo1<foo1<P1, P2>> : std::true_type {};
template <typename T> concept Foo1Type = is_foo1<T>::value;

// concept for foo2:
template <class T> struct is_foo2 : std::false_type {};
template <class P1, class P2> struct is_foo2<foo2<P1, P2>> : std::true_type {};
template <typename T> concept Foo2Type = is_foo2<T>::value;

然后 func 过载变得更简单:

void func(Foo1Type auto a) {    
    a.lol();
    a.lol();
}

void func(Foo2Type auto a) {    
    a.lol();
}

demo

The template parameters are used. Without them, foo1 and foo2 are just class templates and not a classes.

A simple way to minimize the typing would be to use template parameter packs:

template<class... T>
void func(foo1<T...> a) {
    a.lol();
    a.lol();
}

template<class... T>
void func(foo2<T...> a) {
    a.lol();
}

If you need this check a lot, you can create concepts (since C++20):

#include <type_traits>

// concept for foo1:
template <class T> struct is_foo1 : std::false_type {};
template <class P1, class P2> struct is_foo1<foo1<P1, P2>> : std::true_type {};
template <typename T> concept Foo1Type = is_foo1<T>::value;

// concept for foo2:
template <class T> struct is_foo2 : std::false_type {};
template <class P1, class P2> struct is_foo2<foo2<P1, P2>> : std::true_type {};
template <typename T> concept Foo2Type = is_foo2<T>::value;

Then the func overloads become simpler:

void func(Foo1Type auto a) {    
    a.lol();
    a.lol();
}

void func(Foo2Type auto a) {    
    a.lol();
}

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文