关于C++中的typedef typename的含义
用于告诉编译器,我要使用的使类型。
假设
C++
template <typename CONFIG>
class Table {
typedef typename CONFIG::Value VALUE;
public:
void insert(VALUE v);
};
如果有一个结构体声明如下
C++
struct IntConfig {
typedef int Value;
};
那么声明一个Table变量如下
C++
Table<IntConfig> t;
编译器在展开后会得到如下
C++
typedef int VALUE;
现在C++标准更倾向于使用
C++
using VALUE = typename CONFIG::Value;
在JVM hotspot中,很多表的实现采用了上述的方式,以符号表为例说明
C++
template <typename CONFIG, MEMFLAGS F>
class ConcurrentHashTable : public CHeapObj<F> {
typedef typename CONFIG::Value VALUE;
private:
// This is the internal node structure.
// Only constructed with placement new from memory allocated with MEMFLAGS of
// the InternalTable or user-defined memory.
class Node {
private:
Node * volatile _next;
VALUE _value;
很明显,这里使用了模板类,然后在VALUE _value需要替换为CONFIG::Value
这里的typedef typename告诉编译器我要使用的使CONFIG类的Value类型
假设
C++
typedef ConcurrentHashTable<SymbolTableConfig, mtSymbol> SymbolTableHash;
class SymbolTableConfig : public AllStatic {
private:
public:
typedef Symbol* Value; // value of the Node in the hashtable
可以看出,在SymbolTableConfig中定义了Value类型为Symbol*
于是对于ConcurrentHashTable中的Node类中的VALUE值就是Symbol*