c++已知大小的 typedefed 数组的函数模板专门化
请考虑以下代码:
#include <iostream>
#include <typeinfo>
template< typename Type >
void func( Type var )
{
std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is SCALAR. Size = " << sizeof( Type ) << std::endl;
}
#if 1
template< typename Type >
void func( Type * var )
{
std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is ARRAY. Size = " << sizeof( Type * ) << std::endl;
}
#endif
int main( )
{
typedef char char16[ 16 ];
char16 c16 = "16 bytes chars.";
std::cout << "Size of char16 = " << sizeof( char16 ) << std::endl;
func( c16 );
return 0;
}
如果我编译并运行它,我会看到以下内容:
> g++ -Wall -g3 spec_f_pointer.cpp -o spec_f_pointer
> ./spec_f_pointer
Size of char16 = 16
func: var = 16 bytes chars. [Pc].
-> var is ARRAY. Size = 8
显然, func
中打印的 sizeof
指的是指针的大小,而不是指针的大小typedef
数组,如 main()
中给出。
现在我想知道如何正确地实现让我的 func 专门化的技巧,以便它正确地了解我的 typedef 及其大小。
请问这里有人可以帮助我吗?
真的很感谢。
编辑
实现专业化为:
template< typename Type >
void func( Type * const &var )
{
std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is ARRAY. Size = " << sizeof( Type * ) << std::endl;
}
输出为:
Size of char16 = 16
func: var = 16 bytes chars. [A16_c].
-> var is SCALAR. Size = 16
我注意到类型从 Pc
更改为 A16_c
。 有帮助吗?
Please consider the following code:
#include <iostream>
#include <typeinfo>
template< typename Type >
void func( Type var )
{
std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is SCALAR. Size = " << sizeof( Type ) << std::endl;
}
#if 1
template< typename Type >
void func( Type * var )
{
std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is ARRAY. Size = " << sizeof( Type * ) << std::endl;
}
#endif
int main( )
{
typedef char char16[ 16 ];
char16 c16 = "16 bytes chars.";
std::cout << "Size of char16 = " << sizeof( char16 ) << std::endl;
func( c16 );
return 0;
}
If I compile it and run, I see this:
> g++ -Wall -g3 spec_f_pointer.cpp -o spec_f_pointer
> ./spec_f_pointer
Size of char16 = 16
func: var = 16 bytes chars. [Pc].
-> var is ARRAY. Size = 8
Clearly the sizeof
printed inside func
refers to the size of a pointer, and not the size of the typedef
array, as given in main()
.
Now I wonder how to correctly do the trick for getting my func
to specialize in such a way that it correctly knows about my typedef and its size.
Does anyone here can help me, please?
Really thanks.
EDIT
Implementing a specialization as:
template< typename Type >
void func( Type * const &var )
{
std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is ARRAY. Size = " << sizeof( Type * ) << std::endl;
}
The output is:
Size of char16 = 16
func: var = 16 bytes chars. [A16_c].
-> var is SCALAR. Size = 16
I noticed the type change from Pc
to A16_c
.
Does it help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将函数专门用于数组,请执行以下操作:
If you want to specialize your function for arrays, do this:
当用作右值表达式时,数组会衰减为指向第一个元素的指针。您定义的函数采用一个指针并执行预期的操作。如果您想将数组维护为数组,您需要通过引用传递它,并且因为元素数量是类型的一部分,您可能希望将其用作另一个模板参数:
When used as rvalue expressions, arrays decay to pointers to the first element. The function that you have defined takes a pointer and does what is expected. If you want to maintain the array as an array you need to pass it by reference, and because the number of elements is part of the type you probably want to use that as another template argument: