【发布时间】:2018-12-07 20:18:04
【问题描述】:
我正在尝试编写一个类方法,该方法可以根据 DataType 结构变量中保存的值返回任何一个成员变量值。我试过下面的代码:
#include <iostream>
struct A
{
int DataType;/* holds enum value for one of below data data needs to be returned */
union /* value of var is one of these needs to be returned */
{
int32_t x;
uint32_t y;
uint64_t mz;
bool b;
struct
{
char* ptr;
int len;
} str;
}data;
};
struct A a1 = { /* value 1 means int32_t, 2 means uint32_t, 3 means uint64_t , 4 means bool and 5 means str */ 2, 32};
//struct A a2 = { /* value 1 means int32_t, 2 means uint32_t, 3 means uint64_t , 4 means bool and 5 means str */ 5, {{ "Hello",5}}};
class B
{
public:
B(){}
template <>
T GetVar(struct A a0)
{
if (a0.DataType == 2)
return a0.data.y;
if (a0.DataType == 5)
return std::string(a0.data.str.ptr);
return 0;
}
};
int main()
{
B b1;
auto d = b1.GetVar(a1);
std::cout << d << std::endl;
//auto d1 = b1.GetVar(a2);
std::cout << d << std::endl;
}
我得到编译错误 - 我知道错误与初始化 struct A 的 str 成员变量有关(如何解决这个问题)以及类如何方法返回不同的变量值?
$ c++ -std=c++11 try72.cpp
try72.cpp:26:11: error: explicit specialization in non-namespace scope 'class B'
template <>
^
try72.cpp:27:1: error: 'T' does not name a type
T GetVar(struct A a0)
^
try72.cpp: In function 'int main()':
try72.cpp:40:13: error: 'class B' has no member named 'GetVar'
auto d = b1.GetVar(a1);
^
try72.cpp:42:14: error: 'class B' has no member named 'GetVar'
auto d1 = b1.GetVar(a2);
^
try72.cpp:42:21: error: 'a2' was not declared in this scope
auto d1 = b1.GetVar(a2);
^
【问题讨论】:
-
编译器是正确的,你永远不会在任何地方声明
T。无论如何,您不能从单个函数返回不同的类型。您可能想查看std::variant或std::any。或者甚至可以使用union作为返回值? -
@Programmer:您可以查看我为类似问题编写的答案。 stackoverflow.com/questions/53590671/….
-
@P.W - 谢谢,但 std::variant 在 C++ 17 中,我正在使用 C++11 - 有没有办法实现目标?
-
那么您唯一的解决方案是 Boost 库(
boost::variant或boost::any)或联合。 -
@Programmer:我同意 SomeProgrammerDude 的建议。