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()
.
显然,在func中打印的size表示指针的大小,而不是main()中给出的typedef数组的大小。
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.
现在我想知道如何正确地完成使我的func专门化到它正确地知道我的类型定义和它的大小。
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?
我注意到类型从Pc变成了A16_c。它有帮助吗?
2 个解决方案
#1
12
If you want to specialize your function for arrays, do this:
如果你想将你的函数专门化用于数组,请这样做:
template<typename T, int N>
void func(T(&var)[N])
{
typedef T Type[N];
std::cout << __FUNCTION__ << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is ARRAY. Size = " << sizeof( Type ) << std::endl;
std::cout << "Number of elements: " << N << std::endl;
std::cout << "Size of each element: " << sizeof(T) << std::endl;
}
#2
1
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:
当被用作rvalue表达式时,数组会衰变为指向第一个元素的指针。您定义的函数接受一个指针并执行预期的操作。如果你想将数组作为一个数组来维护,你需要通过引用传递它,因为元素的数量是类型的一部分,你可能想把它作为另一个模板参数:
template <typename T, int N>
void f( T(&arg)[N] ) {
cout << sizeof arg << endl;
}
#1
12
If you want to specialize your function for arrays, do this:
如果你想将你的函数专门化用于数组,请这样做:
template<typename T, int N>
void func(T(&var)[N])
{
typedef T Type[N];
std::cout << __FUNCTION__ << " [" << typeid( var ).name( ) << "]." << std::endl;
std::cout << "-> var is ARRAY. Size = " << sizeof( Type ) << std::endl;
std::cout << "Number of elements: " << N << std::endl;
std::cout << "Size of each element: " << sizeof(T) << std::endl;
}
#2
1
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:
当被用作rvalue表达式时,数组会衰变为指向第一个元素的指针。您定义的函数接受一个指针并执行预期的操作。如果你想将数组作为一个数组来维护,你需要通过引用传递它,因为元素的数量是类型的一部分,你可能想把它作为另一个模板参数:
template <typename T, int N>
void f( T(&arg)[N] ) {
cout << sizeof arg << endl;
}