有一个模板函数
template<typename T>
void function(T num){
T tmp;
memset(&tmp,0,sizeof(tmp)); //现在我是这么初始化的
}
然后我需要在函数里定义一个临时变量
T tmp
但这个T有可能是类类型(string), 也可能是内建类型(int, double)
那我要怎么初始化?
现在我是用memset()初始化的;
但如果sum这对象不在一块连续内存或者他定义时会初始化一些特殊成员, 这种方法就不行...
Uniform initialization for c++11:
Before you should be able to write:
@Shihira
I understand what the questioner means is that if it can be initialized with 0 as a parameter, try to initialize it with 0, and if it doesn't work, use the default constructor.
There are several points here. One is that it cannot be
T tmp
directly, because the Scalar Type will not be initialized and will directly use the garbage data in the stack, so that its data is not 0. Second, you cannot use the C methodmemset
, because C++ objects are often constructed by default to allocate memory. If the pointer is set to zero, a segmentation fault or memory leak will occur. Third, because an object is returned directly, performance may be reduced if T does not have a move constructor.The principle of the above writing method is this. Taking advantage of the difference between Int and int, because C++ will give priority to more specialized functions (or overloaded functions) when making template decisions, the following
init_obj
takes precedence over the above one. Enable is used to generate derivation errors for types that cannot be initialized with 0. C++'s SFINAE policy transfers it to the above oneinit_obj
;0.f
is used here because integer types will be implicitly converted to pointers, but float will not, And float can be implicitly converted to any numeric type.Personally, I think my writing is ugly and I’d like to ask for a better way of writing (
For C++11, the writing method above is great:)