英文:
Is it possible to return a member variable of a class specified by a template function?
问题
I am trying to generalize a function for a game engine I am writing to simplify the shader loading process.
Anyway, the difficulty arises in my attempts to templating the function. I attempted to adapt the solution found in this stack thread, and resulted in various versions of the following code without any ideal solutions:
template<typename T, typename U, U T::* x>
U testFunc(T& t)
{
// Simplification of the extremely generalized function;
// I want to return/utilize member variable x of input t
return x;
}
struct testS1 { int a = 100; };
struct testS2 { bool b = true; };
testS1 tS1;
testS2 tS2;
// would ideally return 100
int c = testFunc<testS1, int, &testS1::a>(tS1);
// would ideally return true
bool d = testFunc<testS2, bool, &testS2::b>(tS2);
Running the program gives the following error:
Severity: Error
Line: 46
Code: C2440
Description: 'return': cannot convert from 'int testS1::* ' to 'U'
I understand that the returned value x
is not the same type as U
because int testS1::*
is not the same type as int
.
However, I do not understand what syntax I would use to return the member variable x
of type U
from the struct T
.
The real structs behind the placeholders testS1
and testS2
are very different from each other, so I would like to avoid using a base class/struct if at all possible.
英文:
I am trying to generalize a function for a game engine I am writing to simplify the shader loading process.
Anyway, the difficulty arises in my attempts to templating the function. I attempted to adapt the solution found in this stack thread, and resulted in various versions of the following code without any ideal solutions:
template<typename T, typename U, U T::* x>
U testFunc(T& t)
{
// Simplification of the extremely generalized function;
// I want to return/utilize member variable x of input t
return x;
}
struct testS1 { int a = 100; };
struct testS2 { bool b = true; };
testS1 tS1;
testS2 tS2;
// would ideally return 100
int c = testFunc<testS1, int, &testS1::a>(tS1);
// would ideally return true
bool d = testFunc<testS2, bool, &testS2::b>(tS2);
Running the program gives the following error:
Severity: Error
Line: 46
Code: C2440
Description: 'return': cannot convert from 'int testS1::* ' to 'U'
I understand that the returned value x
is not the same type as U
because int testS1::*
is not the same type as int
.
However, I do not understand what syntax I would use to return the member variable x
of type U
from the struct T
.
The real structs behind the placeholders testS1
and testS2
are very different from each other, so I would like to avoid using a base class/struct if at all possible.
答案1
得分: 4
你只需对 x
进行解引用,就可以获得一个 U
。
英文:
template<class T, class U, U T::*x>
U testFunc(T& t)
{
return t.*x;
}
You simply need to dereference x
to get a U
.
答案2
得分: 4
以下是翻译好的部分:
"另一个答案已经提供了访问成员数据指针的语法。在[tag:C++20]中,使用缩写函数模板,你可以以更简洁的方式完成相同的操作。
template<auto x>
constexpr auto testFunc(auto const& t)
{
return t.*x;
}
现在你可以以更简洁的方式调用,不需要明确的模板参数:
int c = testFunc<&testS1::a>(tS1);
bool d = testFunc<&testS2::b>(tS2);
// ...
英文:
The other answer has already provided the syntax for accessing the pointer to the member data. In [tag:C++20] using abbreviated function template, you could do it, less verbosely the same
template<auto x>
constexpr auto testFunc(auto const& t)
{
return t.*x;
}
Now you call with less explicate template arguments
int c = testFunc<&testS1::a>(tS1);
bool d = testFunc<&testS2::b>(tS2);
// ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论