指向类成员的指针作为模板参数

编程入门 行业动态 更新时间:2024-10-25 20:24:20
本文介绍了指向类成员的指针作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用类成员的指针作为模板参数,如:

I want to use a pointer to a class member as a template parameter as in:

template <class Class, class Result, Result Class::*Member> struct MyStruct { // ... };

使用 MyStruct< SomeClass,SomeResult,SomeClass ::值>变量工作正常,但我不喜欢,我必须指定 SomeClass 和 SomeResult 。

Using this struct like MyStruct<SomeClass, SomeResult, &SomeClass::value> variable works just fine, but I don't like that I have to specify SomeClass and SomeResult.

我想使用 MyStruct<& SomeClass :: value>

I would like to use MyStruct<&SomeClass::value> variable if that is possible, but without losing the ability to pass any class and have any result type.

我尝试了下面的方法,但是如果可能的话,变量语法是非法的:

I tried the following, but the syntax is illegal:

template <class Class, class Result> template <Result Class::*Member> struct MyStruct { // ... };

错误:模板参数列表过多

error: too many template-parameter-lists

我尝试使用一个辅助函数(实际上在Clang中工作,但被GCC拒绝):

I tried using a helper function (that does actually work in Clang but is refused by GCC):

template <class Class, class Result> static constexpr auto makeMyStruct(Result Class::*member) -> MyStruct<Class, Result, member> { // ... }

错误:函数体外使用参数`member'错误:模板参数3无效

error: use of parameter `member' outside function body error: template argument 3 is invalid

是否可以有一个简单的 MyStruct<& SomeClass :: value> ,如果是,如何?

Is it possible to have a simple MyStruct<&SomeClass::value>, and if so, how?

相关问题没有解决我的问题:

Related question that did not solve my question:

  • 类成员的指针作为模板参数
  • 使用constexpr返回模板函数的C ++ 0x错误
  • Pointer to class member as template parameter
  • C++0x error with constexpr and returning template function
推荐答案

在本文中为下一个即将到来的C ++标准提出了我的问题的答案:

An answer to my question was proposed in this paper for the next upcoming C++ standard:

  • isocpp/blog/2013/03/n3601-implicit-template-parameters
  • isocpp/blog/2013/03/n3601-implicit-template-parameters

已建议此语法:

template<using typename T, T t> struct some_struct { /* ... */ }; some_struct<&A::f> x;

需要一个新的语法结构表明你现在不能这样做。

The need for a new syntactical construct indicates that you cannot do that by now.

我希望n3601将被接受。 : - )

I hope n3601 will be accepted. :-)

更多推荐

指向类成员的指针作为模板参数

本文发布于:2023-07-28 02:43:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   成员   模板   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!