模板函数:C++ 中默认构造,无需复制构造;
考虑
struct C {
C() { printf("C::C()\n" ); }
C(int) { printf("C::C(int)\n" ); }
C( const C& ) { printf("copy-constructed\n"); }
};
到模板函数
template< typename T > void foo(){
// default-construct a temporary variable of type T
// this is what the question is about.
T t1; // will be uninitialized for e.g. int, float, ...
T t2 = T(); // will call default constructor, then copy constructor... :(
T t3(); // deception: this is a local function declaration :(
}
int main(){
foo<int>();
foo<C >();
}
查看t1
,当T
为例如int
时,它不会被初始化。另一方面,t2
将从默认构造的临时复制构造。
问题:除了使用 template-fu 之外,在 C++ 中是否可以默认构造泛型变量?
Considering
struct C {
C() { printf("C::C()\n" ); }
C(int) { printf("C::C(int)\n" ); }
C( const C& ) { printf("copy-constructed\n"); }
};
And a template function
template< typename T > void foo(){
// default-construct a temporary variable of type T
// this is what the question is about.
T t1; // will be uninitialized for e.g. int, float, ...
T t2 = T(); // will call default constructor, then copy constructor... :(
T t3(); // deception: this is a local function declaration :(
}
int main(){
foo<int>();
foo<C >();
}
Looking at t1
, it will not be initialized when T
is e.g. int
. On the other hand, t2
will be copy-constructed from a default constructed temporary.
The question: is it possible in C++ to default-construct a generic variable, other than with template-fu?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个你可以使用的技巧,使用本地类:
我想我从另一个 Stack Overflow 问题的答案中读到了这个技巧,但我目前无法找到该问题。
或者,您可以使用
boost::value_initialized
类模板,它基本上执行相同的操作,具有更大的灵活性和一致性,并针对有缺陷的编译器提供了解决方法。在 C++0x 中,这要容易得多:您可以使用空的初始值设定项列表:(
据我所知,只有 gcc 4.5+ 支持 C++0x 初始值设定项列表。Clang 和 Visual C++ 尚不支持它们。)
Here's a trick you can use, using a local class:
I think I read about this trick from an answer to another Stack Overflow question, but I am unable to find that question at the moment.
Alternatively, you can use the
boost::value_initialized<T>
class template, which basically does the same thing, with greater flexibility and consistency and with workarounds for buggy compilers.In C++0x, it's much easier: you can use an empty initializer list:
(To the best of my knowledge, only gcc 4.5+ supports C++0x initializer lists. Clang and Visual C++ don't yet support them.)
如果您不关心复制构造函数必须存在这一事实,而只是想阻止它被调用:
不用担心:它不会存在。在这种情况下,复制构造函数调用将被省略。始终可靠 - 即使在禁用优化的情况下进行编译 (
-O0
)。If you don’t care for the fact that the copy constructor must exist, and just want to prevent it being called:
Don’t worry: it won’t be. The copy constructor call will be elided in this situation. Always, and reliably – even when you compile with optimizations disabled (
-O0
).你真正的问题是什么?为 t1 实例调用默认构造函数。
What is your real question? The default constructor is called for t1 instance.
不在我的编译器(VC2008)上。我的输出是...
这就是我期望它做的事情。我错过了什么吗?
Not on my compiler (VC2008). Output for me is...
Which is what I'd expect it to do. Am I missing something?