我想使用类成员的指针作为模板参数,如:
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. :-)
更多推荐
指向类成员的指针作为模板参数
发布评论