- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码:
#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]
.
这解释了为什么 target
是 char
的数组, 和 source
是 char
的数组数组
关于C++内部变量的奇怪模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9131684/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!