我有一个Object[]数组,我试图找到原始的数组.我试过用Class.isPrimitive(),但似乎我做错了什么:
int i = 3;
Object o = i;
System.out.println(o.getClass().getName() + ", " +
o.getClass().isPrimitive());
Run Code Online (Sandbox Code Playgroud)
打印java.lang.Integer, false.
有正确的方法或替代方案吗?
我需要一个C++模板,给定一个类型和该类型的对象,它可以根据类型是否为整数做出决定,同时能够访问实际对象.我试过这个
template <typename T, T &N>
struct C {
enum { Value = 0 };
};
template <int &N>
struct C<int, N> {
enum { Value = N };
};
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有什么方法可以实现类似的东西吗?
编辑
我试图实现的是这样的,这将在编译时发生:
if (type is int) {
return IntWrapper<int_value>
else {
return type
}
Run Code Online (Sandbox Code Playgroud)
您实际上可以在模板实例化中传递指针或对象的引用,如下所示:
struct X {
static const int Value = 5;
};
template <X *x>
struct C {
static const int Value = (*x).Value;
};
X x;
std::cout << C<&x>::Value << std::endl; // prints 5
Run Code Online (Sandbox Code Playgroud)
但显然所有这一切都是通过推断 …