gpt4 book ai didi

C++内部变量的奇怪模板特化

转载 作者:行者123 更新时间:2023-11-28 08:10:47 26 4
gpt4 key购买 nike

考虑以下代码:

#include    <iostream>
#include <typeinfo>
#include <cstring>
#include <cstdlib>

static void random_string( char *s, const int len )
{
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

for ( int i = 0; i < len; ++i )
s[ i ] = alphanum[ rand( ) % ( sizeof( alphanum ) - 1 ) ];

s[ len ] = 0;
}

template< typename Type, int Length >
void info( Type ( &var )[ Length ] )
{
std::cout << "-> Type is " << typeid( var ).name( ) << std::endl;
std::cout << "-> Pointer size: " << sizeof( Type * ) << std::endl;
std::cout << "-> Number of elements: " << Length << std::endl;
std::cout << "-> Size of each element: " << sizeof( Type ) << std::endl;
std::cout << "-> contents: \"" << var << "\"." << std::endl;
}


template< typename SourceType, int SourceLength, typename TargetType, int TargetLength >
void func( TargetType ( &target )[ TargetLength ] )
{
SourceType source[ SourceLength ];

random_string( ( char * )source, SourceLength - 1 );

std::cout << "-= source =-" << std::endl;
info( source );

std::cout << "-= target =-" << std::endl;
info( target );

std::memcpy( target, source,
( SourceLength < TargetLength ? SourceLength : TargetLength ) );
}

int main( )
{
typedef char char16[ 16 ];
typedef char char64[ 64 ];

char16 c16 = "16 bytes chars.";
char64 c64 = "64 bytes chars. There's a lot more room here...";

std::cout << "c16 = \"" << c16 << "\"." << std::endl;
func< char64, sizeof( char64 ) >( c16 );
std::cout << "c16 = \"" << c16 << "\"." << std::endl;

std::cout << "c64 = \"" << c64 << "\"." << std::endl;
func< char16, sizeof( char16 ) >( c64 );
std::cout << "c64 = \"" << c64 << "\"." << std::endl;

return 0;
}

这个输出:

> g++ -Wall -g3 array_conversions.cpp -o array_conversions
> ./array_inner_type
c16 = "16 bytes chars.".
-= source =-
-> Type is A64_A64_c
-> Pointer size: 8
-> Number of elements: 64
-> Size of each element: 64
-> contents: "0x7fff8b913380".
-= target =-
-> Type is A16_c
-> Pointer size: 8
-> Number of elements: 16
-> Size of each element: 1
-> contents: "16 bytes chars.".
c16 = "fa37JncCHryDsbza".
c64 = "64 bytes chars. There's a lot more room here...".
-= source =-
-> Type is A16_A16_c
-> Pointer size: 8
-> Number of elements: 16
-> Size of each element: 16
-> contents: "0x7fff8b914280".
-= target =-
-> Type is A64_c
-> Pointer size: 8
-> Number of elements: 64
-> Size of each element: 1
-> contents: "64 bytes chars. There's a lot more room here...".
c64 = "Z2nOXpPIhMFSv8k".

可以看出,我和GCC对SourceType source[ SourceLength ]的理解是不一样的。

我希望 SourceType source[ SourceLength ]char64 ("A64_c") 类型,但 GCC 告诉我它是 "A64_A64_c"并且表现得好像它是char64 或类似内容的数组。

奇怪的是,它只发生在本地 source 变量上。 target 函数参数被正确解释。

我错过了什么?我的代码有什么误解吗?

我觉得这个问题与这个 C++ Template argument changes Reference to Pointer 有关,我大约一年前发布的,但是这个问题无论如何都没有答案,所以现在我请求你的帮助。

非常感谢。

最佳答案

c16类型为 char[16]

func< char64, sizeof( char64 ) >( c16 )实例化 func

SourceType == char64 == char[64]
SourceLength == sizeof( char64 ) == 64
TargetType == char
TargetLength == 16

这意味着target类型为 char (&)[16]source类型为 char[64][64] .

这解释了为什么 targetchar 的数组, 和 sourcechar 的数组数组

关于C++内部变量的奇怪模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9131684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com