为什么在 C++11 中类型参数包之后不允许使用整数值参数包?

发布于 2024-12-26 02:33:40 字数 577 浏览 1 评论 0原文

如果没有例子,这个问题几乎没有意义。这就是我正在尝试做的事情。

一般来说,C++ 允许以下操作:

template<class T, class U, T t, U u>
void func() {}

func<char, int, 'A', 10>();

但它的自然可变参数扩展似乎不起作用。

template<class...T, T... t>
void func() {}

func<char, int, 'A', 10>(); 

clang 和 g++4.7 都拒绝上述代码。该错误显示在实例化完成的位置。在我看来,两个可变参数列表应该明确解析,因为第一个具有类型,另一个仅具有整数值。

如果上面的方法不起作用,我认为下面的方法也不起作用。

template <class Ret, class... Args, Ret (*func)(Args...)>
class Foo {};

我认为 Foo 模板是一个相当有用的东西。

The question almost does not make sense without an example. So here is what I'm trying to do.

In general C++ allows the following:

template<class T, class U, T t, U u>
void func() {}

func<char, int, 'A', 10>();

But it seems like its natural variadic extension does not work.

template<class...T, T... t>
void func() {}

func<char, int, 'A', 10>(); 

Both clang and g++4.7 reject the above code. The error is shown where the instantiation is done. It appears to me that the two variadic lists should be parsed unambiguously because the first one has types and the other one has integral values only.

If the above is not meant to work, I think the following won't work either.

template <class Ret, class... Args, Ret (*func)(Args...)>
class Foo {};

I think the Foo template is a rather useful thing to have.

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

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

发布评论

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

评论(2

番薯 2025-01-02 02:33:40

额外:直接回答你的第一个问题,你也可以将 templatevoid func() {} 变成模板-inside-a-template。这在 g++4.6 中不起作用,但在 clang 3.0 中起作用,因此我花了一段时间才找到它。)

将模板放入模板中:

template<class ... T>
struct func_types {
   template <T ... t>
   static void func_values() {
       // This next line is just a demonstration, and 
       // would need to be changed for other types:
       printf("%c %d\n", t...);
   }
};

int main() {
    func_types<char, int> :: func_values<'A', 10>();
}

是一个 template-inside-a-模板可以接受吗?另一种方法是使用带有 func<; 的元组。元组, make_tuple('A',10) >.我认为这是可行的,但您可能必须推出自己的元组类(看来 make_tuple 不是 constexpr

最后,您也许可以按如下方式实现 Foo 模板:

template<typename Ret, typename ...Args>
struct Foo {
        template< Ret (*func)(Args...)>
        struct Bar {
                template<typename T...>
                Bar(T&&... args) {
                        cout << "executing the function gives: "
                           << func(std::forward(args)...) << endl;
                }
        };
};

int main () {
    Foo<size_t, const char*> ::  Bar<strlen> test1("hi");
    Foo<int, const char*, const char*> ::  Bar<strcmp> test2("compare","these");
}

后者代码位于 ideone 上。为了演示,我实现了一个构造函数,将参数转发到模板中的代码函数。

(Extra: Directly answering your first question, you can also turn template<class...T, T... t> void func() {} into a template-inside-a-template. This doesn't work in g++4.6, but does in clang 3.0, hence it took me a while to find it.)

Put a template inside a template:

template<class ... T>
struct func_types {
   template <T ... t>
   static void func_values() {
       // This next line is just a demonstration, and 
       // would need to be changed for other types:
       printf("%c %d\n", t...);
   }
};

int main() {
    func_types<char, int> :: func_values<'A', 10>();
}

Is a template-inside-a-template acceptable? An alternative is to use tuples with func< tuple<char,int> , make_tuple('A',10) >. I think this is doable, but you might have to roll your own tuple class (it appears that make_tuple isn't a constexpr.

Finally, you might be able to implement your Foo template as follows:

template<typename Ret, typename ...Args>
struct Foo {
        template< Ret (*func)(Args...)>
        struct Bar {
                template<typename T...>
                Bar(T&&... args) {
                        cout << "executing the function gives: "
                           << func(std::forward(args)...) << endl;
                }
        };
};

int main () {
    Foo<size_t, const char*> ::  Bar<strlen> test1("hi");
    Foo<int, const char*, const char*> ::  Bar<strcmp> test2("compare","these");
}

This latter code is on ideone. To demonstrate, I implemented a constructor to forward the args to the function that's code into the template.

一生独一 2025-01-02 02:33:40

因为没有人认为拥有这个功能是值得的。可变参数模板的设计旨在简单且有效。其他潜在的高级和有用的功能也不包括在内。

Because noone has thought it would be worth to have this feature. The design of variadic template was intended to be simple and working. Other potentially advanced and useful features werent included either.

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