可能的编译器错误,本地结构从模板参数(phew)访问函数中的静态变量 如果有人注释了标记行,为什么有人解释为什么下面的代码在VisualStudioC++ 2008/2010上不起作用?是编译器错误还是我做错了什么?代码应该输出一个10U的序列,但删除该行后,它不会输出任何内容。变量chr在FilledArray构造函数中变为0。谢谢 // returns a c-string which contains the char repeated a certain number of times template<char THE_CHAR> const char *RepeatChar(unsigned int uiNumber) { // must be static so can be accessed by struct static const unsigned int uiMaxSize = 1024; static const char chr = THE_CHAR; char temp = chr; // comment out this line and it doesn't work assert(uiMaxSize > 0); assert(uiNumber <= uiMaxSize); static const struct FilledArray { char data[uiMaxSize + 1]; FilledArray() { // fill all but last with the char (space for terminator) fill(data, data + uiMaxSize, chr); data[uiMaxSize] = '\0'; } } sFilledArray; // clever bit return sFilledArray.data + uiMaxSize - uiNumber; } int main() { cout << RepeatChar<'U'>(10) << endl; } //返回一个c字符串,其中包含重复一定次数的字符 模板 常量字符*重复字符(无符号整数) { //必须是静态的,以便可以通过结构访问 静态常量unsigned int uiMaxSize=1024; 静态常量char chr=_字符; char temp=chr;//注释掉这行,它就不起作用了 断言(uiMaxSize>0); assert(uiNumber
看起来像一个编译器错误。我将代码更改为以下内容:可能的编译器错误,本地结构从模板参数(phew)访问函数中的静态变量 如果有人注释了标记行,为什么有人解释为什么下面的代码在VisualStudioC++ 2008/2010上不起作用?是编译器错误还是我做错了什么?代码应该输出一个10U的序列,但删除该行后,它不会输出任何内容。变量chr在FilledArray构造函数中变为0。谢谢 // returns a c-string which contains the char repeated a certain number of times template<char THE_CHAR> const char *RepeatChar(unsigned int uiNumber) { // must be static so can be accessed by struct static const unsigned int uiMaxSize = 1024; static const char chr = THE_CHAR; char temp = chr; // comment out this line and it doesn't work assert(uiMaxSize > 0); assert(uiNumber <= uiMaxSize); static const struct FilledArray { char data[uiMaxSize + 1]; FilledArray() { // fill all but last with the char (space for terminator) fill(data, data + uiMaxSize, chr); data[uiMaxSize] = '\0'; } } sFilledArray; // clever bit return sFilledArray.data + uiMaxSize - uiNumber; } int main() { cout << RepeatChar<'U'>(10) << endl; } //返回一个c字符串,其中包含重复一定次数的字符 模板 常量字符*重复字符(无符号整数) { //必须是静态的,以便可以通过结构访问 静态常量unsigned int uiMaxSize=1024; 静态常量char chr=_字符; char temp=chr;//注释掉这行,它就不起作用了 断言(uiMaxSize>0); assert(uiNumber,c++,visual-studio,C++,Visual Studio,看起来像一个编译器错误。我将代码更改为以下内容: #include <iostream> #include <assert.h> using namespace std; // returns a c-string which contains the char repeated a certain number of times template<char THE_CHAR> const char *RepeatChar(unsigned int uiNu
#include <iostream>
#include <assert.h>
using namespace std;
// returns a c-string which contains the char repeated a certain number of times
template<char THE_CHAR>
const char *RepeatChar(unsigned int uiNumber)
{
// must be static so can be accessed by struct
static const unsigned int uiMaxSize = 1024;
static const char chr = THE_CHAR;
// char temp = chr; // comment out this line and it doesn't work
static const struct FilledArray
{
char data[uiMaxSize + 1];
FilledArray()
{
cout << "[" << chr << "]" << endl;
fill(data, data + uiMaxSize, chr);
data[uiMaxSize] = '\0';
}
} sFilledArray;
return sFilledArray.data + uiMaxSize - uiNumber;
}
int main()
{
cout << RepeatChar<'U'>(10) << endl;
}
但是,当用作fill中的参数时,它使用offsetRepeatChar'::
2'::chr(0FFE3BAh),如中所示:
00FA32AA推送偏移量'RepeatChar':'2'::chr(0FFE3BAh)
但内存位置0FFE3BAh包含一个0。但如果chr在其他位置使用,则它在该位置填充55h
看起来生成static部分的代码认为它已经完全优化了chr,但实际上并没有
不幸的是,我们没有VC++的源代码,所以在微软修复之前,这仍然是一个谜
编辑:
同样的代码在g++4.4.5中不编译。实际上,它编译得很好,但生成的代码依赖于它似乎遗漏的外部代码,这会导致链接器呕吐。在使用g++编译时,我得到以下错误代码:
/tmp/cccGJmv9.o: In function `char const* RepeatChar<(char)85>(unsigned int)::FilledArray::FilledArray()':
b1.cpp:(.text+0x56): undefined reference to `chr'
b1.cpp:(.text+0xa5): undefined reference to `uiMaxSize'
b1.cpp:(.text+0xb2): undefined reference to `chr'
b1.cpp:(.text+0xc1): undefined reference to `uiMaxSize'
collect2: ld returned 1 exit status
/tmp/cccGJmv9.o:在函数'char const*RepeatChar(unsigned int)::filledaray::FilledArray()'中:
b1.cpp:(.text+0x56):对“chr”的未定义引用
b1.cpp:(.text+0xa5):对“uiMaxSize”的未定义引用
b1.cpp:(.text+0xb2):对“chr”的未定义引用
b1.cpp:(.text+0xc1):对“uiMaxSize”的未定义引用
collect2:ld返回了1个退出状态
这与诊断一致,即它似乎认为它优化了静态常量变量,但实际上没有。看起来像是一个编译器错误……尽管问题是:
chr
带来了什么附加值?即,为什么不编写填充(data,data+uiMaxSize,the_CHAR)
直接?几乎总是当它是一个编译器错误时,它不是。数据[uiMaxSize+1]='\0';
应该是数据[uiMaxSize]='\0';
谢谢。虽然我已经解决了这个问题,但原始问题仍然存在。gcc 4.7.2的行为方式类似,即使使用字符作为fill()的参数
。谢谢!我不太了解程序集,但很高兴知道它不是我代码中的错误。我将以不同的方式编写它。
cout << "[" << chr << "]" << endl;
00FA3273 push offset std::endl (0F9E730h)
00FA3278 push offset string "]" (0FFE3C0h)
00FA327D push 55h
00FA327F push offset string "[" (0FFE3BCh)
00FA3284 push offset std::cout (1011F80h)
00FA3289 call std::operator<<<std::char_traits<char> > (0F9EF91h)
00FA328E add esp,8
00FA3291 push eax
00FA3292 call std::operator<<<std::char_traits<char> > (0F9FA3Bh)
00FA3297 add esp,8
00FA329A push eax
00FA329B call std::operator<<<std::char_traits<char> > (0F9EF91h)
00FA32A0 add esp,8
00FA32A3 mov ecx,eax
00FA32A5 call std::basic_ostream<char,std::char_traits<char> >::operator<< (0F9EFD2h)
fill(data, data + uiMaxSize, chr);
00FA32AA push offset `RepeatChar<85>'::`2'::chr (0FFE3BAh)
00FA32AF mov eax,dword ptr [this]
00FA32B2 add eax,400h
00FA32B7 push eax
00FA32B8 mov ecx,dword ptr [this]
00FA32BB push ecx
00FA32BC call std::fill<char *,char> (0F9E839h)
00FA32C1 add esp,0Ch
data[uiMaxSize] = '\0';
00FA32C4 mov eax,dword ptr [this]
00FA32C7 mov byte ptr [eax+400h],0
00FA327D push 55h
00FA32AA push offset `RepeatChar<85>'::`2'::chr (0FFE3BAh)
/tmp/cccGJmv9.o: In function `char const* RepeatChar<(char)85>(unsigned int)::FilledArray::FilledArray()':
b1.cpp:(.text+0x56): undefined reference to `chr'
b1.cpp:(.text+0xa5): undefined reference to `uiMaxSize'
b1.cpp:(.text+0xb2): undefined reference to `chr'
b1.cpp:(.text+0xc1): undefined reference to `uiMaxSize'
collect2: ld returned 1 exit status