我希望有一种方法可以为具有大量混合类型的可访问(但不可编辑)数据成员的类编写单个 get 函数。使用包含成员地址的 void*-cast 副本的映射将起作用,如下面的代码所示,但是一旦将“const”加入混合中以强制执行只读,不出所料,C++ 会咆哮说'const void*' 类型不能被重新转换以适当地访问数据成员。以下代码适用于为混合数据类型的类编写单个 get 函数,但它有效地使 get 函数访问的所有数据成员都公开(具体参见类中的get
函数memlist
)。
底线:
有没有办法使指针类型可转换,同时在实际内存位置保持只读?或者更根本的是,可以定义一个指向常量变量的类型可转换指针吗?例如,在我看来,它const type *var
为只读变量定义了一个只读/不可强制转换的地址,而我试图找到更像的东西(到目前为止还没有对我有用)type * const var
,尽管我没有无法找到有关此的任何文档。
#include <iostream>
#include <string>
#include <map>
class A{
public:
A(int a, double b): a(a), b(b) {};
private:
int a;
double b;
friend std::ostream& operator<<(std::ostream& os, A& rhs);
};
class memlist{
public:
memlist(int param1, double param2)
{
myint = new int(param1);
mydouble = new double(param2);
myclass = new A(param1,param2);
getMap["myint"] = myint;
getMap["mydouble"] = mydouble;
getMap["myclass"] = myclass;
}
~memlist()
{
delete myint;
delete mydouble;
delete myclass;
}
void* get(std::string param) {return getMap[param];};
private:
int *myint;
double *mydouble;
A *myclass;
std::map<std::string,void*> getMap;
};
std::ostream& operator<<(std::ostream& os, A& rhs){
os << rhs.a << std::endl << rhs.b;
return os;
};
int main(){
int myint = 5;
double mydbl = 3.14159263;
memlist mymem(myint,mydbl);
std::cout << *(int*)mymem.get("myint") << std::endl;
std::cout << *(double*)mymem.get("mydouble") << std::endl;
std::cout << *(A*)mymem.get("myclass") << std::endl;
*(int*)mymem.get("myint") = 10;
std::cout << *(int*)mymem.get("myint") << std::endl;
return 0;
}
输出:
5
3.14159
5
3.14159
10
显示的代码非常,容我们说,设计不当。
void*
就像在 C++ 中一样接近破坏类型系统。正如评论中提到的,这std::any
是一个更好的解决方案。
也就是说,我认为以类型安全的方式实现您在问题中说明的内容是一项挑战。至少可以说,这太过分了。
#include <iostream>
#include <type_traits>
using namespace std;
template<typename>
struct is_str_literal : false_type {};
template<size_t N>
struct is_str_literal<const char[N]> : true_type {};
template<typename T>
struct is_str_literal<T&> : is_str_literal<T> {};
template<typename T>
constexpr bool is_str_literal_v = is_str_literal<T>::value;
constexpr bool samestr(const char* arr1, const char* arr2, size_t n)
{
return n == 0 ? arr1[0] == arr2[0] :
(arr1[n] == arr2[n]) && samestr(arr1, arr2, n - 1);
}
template<size_t N1, size_t N2>
constexpr bool samestr(const char (&arr1)[N1], const char (&arr2)[N2])
{
return N1 == N2 ? samestr(arr1, arr2, N1 - 1) : false;
}
constexpr char myint[] = "myint";
constexpr char mydouble[] = "mydouble";
constexpr char myclass[] = "myclass";
struct S
{
template<const auto& name>
const auto& get()
{
static_assert(is_str_literal_v<decltype(name)>, "usage: get<var name>()");
if constexpr(samestr(name, ::myint))
return myint;
if constexpr(samestr(name, ::mydouble))
return mydouble;
if constexpr(samestr(name, ::myclass))
return myclass;
}
int myint;
double mydouble;
char myclass;
};
int main()
{
S s;
s.myint = 42;
s.mydouble = 10.0;
s.myclass = 'c';
cout << s.get<myint>() << endl;
cout << s.get<mydouble>() << endl;
cout << s.get<myclass>() << endl;
}
这使用 C++17。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句