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

发布于 2025-01-01 23:03:49 字数 1970 浏览 1 评论 0原文

请考虑以下代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2025-01-08 23:03:49

如果您想将函数专门用于数组,请执行以下操作:

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

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;
}
梦境 2025-01-08 23:03:49

当用作右值表达式时,数组会衰减为指向第一个元素的指针。您定义的函数采用一个指针并执行预期的操作。如果您想将数组维护为数组,您需要通过引用传递它,并且因为元素数量是类型的一部分,您可能希望将其用作另一个模板参数:

template <typename T, int N>
void f( T(&arg)[N] ) {
    cout << sizeof arg << 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文