- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用类型特征来添加对模板参数的引用。
template < class T >
struct S {
typename add_reference< T >::type reference; // reference member should always be a reference
};
...
typedef Bar< Foo > type;
S< type > s; // does not add reference, S:: reference is of type type, not type&
不过好像不行。这是正确的方法吗?我的编译器是 g++ 4.3。谢谢。
澄清:我希望引用成员是引用,无论是否实例化了 S
最佳答案
您忘记了 typedef
。 typename
只是表示您将要使用一个在模板声明处尚未被称为类型的类型名。如果你真的想创建一个 typedef,你实际上还需要那个关键字。而且我认为您在下面使用它时忘记实际命名类型:
template < class T >
struct S {
typedef typename add_reference< T >::type reference;
};
...
typedef Bar< Foo > type;
S< type >::reference s = some_foo; // initialize!
记得初始化引用。如果您事先知道 T
永远不是引用(以避免引用到引用的问题),您也可以直接这样做:
template < class T >
struct S {
typedef T &reference;
};
typedef Bar< Foo > type;
S< type >::reference s = some_bar_foo; // initialize!
如果您想做的是创建一个引用数据成员,那么您没有 typedef
的语法是正确的
template < class T >
struct S {
typename add_reference< T >::type reference;
};
...
typedef Bar< Foo > type;
S< type > s = { some_bar_foo }; // initialize!
s.reference = some_other_bar_foo; // assign "some_other_bar_foo" to "some_bar_foo"
我不知道你到底想做什么。
关于c++ - boost add_reference 不适用于模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2038514/
我正在尝试使用类型特征来添加对模板参数的引用。 template struct S { typename add_reference::type reference; // reference me
我正在查看 [VC10 的] unique_ptr,它们做了一些我不明白的事情: typedef typename tr1::remove_reference::type _Dx_noref; _Dx
我有以下迁移 class LinkDoctorsAndSpecializations 'index name' 中指定的那样 最佳答案 作为我commented , 做: add_index :ta
向上迁移这个迁移 def change remove_reference :order_items, :order, foreign_key: true end 或向下迁移此迁移 def ch
rails 中的"add_foreign_key" 和"add_reference" 方法有什么区别? 根据rails official guide,我的理解是它们都用于在两个表之间创建外键约束。 最
在 Rails 5 和 PostgreSQL 中。 我创建这个文件是为了默认使用 bigint 数据类型而不是 integer: # config/initializers/bigint_primar
我刚开始学习 Rails,如果这个问题的答案很明显,我很抱歉。 我已经在我的应用程序中添加了对帖子和类别表的迁移,现在我使用以下行在我的帖子表中添加对默认值非空的类别的引用: add_referenc
我有以下两个模型: class Store < ActiveRecord::Base belongs_to :person end class Person < ActiveRecord::B
我是一名优秀的程序员,十分优秀!