简体   繁体   English

已知大小的typedef数组的c ++函数模板专门化

[英]c++ function template specialization for known size typedefed array

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内部打印的sizeof是指指针的大小,而不是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专门化的窍门,使其能够正确了解我的typedef及其大小。

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 . 我注意到类型从Pc更改为A16_c Does it help? 有帮助吗?

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;
}

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: 如果要将数组维护为数组,则需要通过引用将其传递,并且由于元素数量是该类型的一部分,因此您可能希望将其用作另一个模板参数:

template <typename T, int N>
void f( T(&arg)[N] ) {
    cout << sizeof arg << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM