C++:如何使用模板函数中的类型进行分支?
我对模板不太熟悉。如何编写名为 get 的模板函数,该函数根据模板类型选择从中获取的数组?请参阅下面的示例:
struct Foo
{
int iArr[10];
char cArr[10];
// How to pick array here based on template type?
template < typename T >
T get( int idx )
{
// This does NOT work!
switch ( T )
{
case int:
return iArr[ idx ];
case char:
return cArr[ idx ];
}
}
};
// Expected behaviour of get()
Foo foo;
int i = foo.get< int >( 2 );
char c = foo.get< char >( 4 );
I am not quite proficient with templates. How do I write the a template function called get that chooses the array it gets from based on the template type? See the example below:
struct Foo
{
int iArr[10];
char cArr[10];
// How to pick array here based on template type?
template < typename T >
T get( int idx )
{
// This does NOT work!
switch ( T )
{
case int:
return iArr[ idx ];
case char:
return cArr[ idx ];
}
}
};
// Expected behaviour of get()
Foo foo;
int i = foo.get< int >( 2 );
char c = foo.get< char >( 4 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然 Jason 提出的解决方案有效,但它远非惯用,并且更难以维护,因为
switch
语句 1) 中的case
值没有明显的含义(“幻数”) ") 和 2) 很容易与switch_value
特化中的值不同步。我建议这样做:使用
int
或char
以外的任何类型调用Foo::get<>
将产生编译器错误。While the solution proposed by Jason works, it's far from idiomatic, and is harder to maintain since the
case
values in theswitch
statement 1) have no apparent meaning ("magic numbers") and 2) could easily get out of sync with the values in theswitch_value<>
specializations. I would propose this instead:Invoking
Foo::get<>
with any type other thanint
orchar
will yield a compiler error.您需要添加某种类型的值结构,您可以使用它来获取 switch 语句的值。例如:
这里的好处是,如果您使用没有有效值的类型,则应该抛出编译器错误,因为
switch_value
的默认版本没有定义成员value
,因此如果您没有专门针对特定类型的结构,那么这样的模板实例化将会失败。You would need to add a value structure of some type you can use to get the values for your switch-statement from. For instance:
The nice thing here is this should throw a compiler error if you use a type that does not have a valid value since the default version of
switch_value<T>
does not define a membervalue
, so if you haven't specialized the structure for a specific type, then such a template instantiation will fail.所有这些答案都太过分了。
All of these answers are wayyyy overkill.
您可以专门化成员函数:
Works with msvc++ 2010。希望这有帮助。
Jason 建议的 switch 专业化也应该可以正常工作。
You could specialise the member function:
Works with msvc++ 2010. Hope this helps.
The switch specialisation suggested by Jason should also work fine.
对于您的示例来说,这可能有点过分了,但如果您确实一次只需要存储一个数组,那么您可以使用 boost::variant 类和访问者,例如,
GetVisitor 类隐含的对变体的限制是变体的所有成员都必须实现:
因此您还可以添加 std::vector 和 std::deque 作为变体的潜在成员。
This is probably overkill for your example, but if you really only need to store one array at a time, then you can use the boost::variant class and a visitor, e.g.,
The restriction on the variant implied by the GetVisitor class is that all members of the variant must implement:
So you could also add, e.g., std::vector and std::deque as potential members of the variant.
我认为除了只关注模板函数之外,这就是您想要的:
在 .h 文件中,
您可以像这样使用它:
I assume this is what you want beside just focusing on template function:
in a .h file
somewhere you use it like: