小编dri*_*l3r的帖子

确定对象是否是基本类型

我有一个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.

有正确的方法或替代方案吗?

java reflection

109
推荐指数
9
解决办法
11万
查看次数

C++模板专业化问题

我需要一个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)

但显然所有这一切都是通过推断 …

c++ templates metaprogramming

3
推荐指数
2
解决办法
1719
查看次数

标签 统计

c++ ×1

java ×1

metaprogramming ×1

reflection ×1

templates ×1