This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] 编译器:错误"const_cast只能调整类型限定符;它不能更改基础类型"尽管基础类型未更改

Guru**** 2616675 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/tools/code-composer-studio-group/ccs/f/code-composer-studio-forum/654558/compiler-error-a-const_cast-can-only-adjust-type-qualifiers-it-cannot-change-the-underlying-type-although-underlying-type-not-changed

工具/软件:TI C/C++编译器

我发现TI的armcl编译器有一些奇怪之处,当变量通过const_cast被转换为typedef时,它报告错误“a const_cast only adjust type qualifiers;it cannot change the underlying type”。 可使用以下示例复制该文档:

extern "C" void (*const interruptvectors[])();

typedef void (*ISR_t)(void);

static ISR_t* source_vector_table = const_cast<ISR_t*>(interruptvectors);

armcl不编译这个,尽管我认为没有错误,因为interruptVector的类型是const ISR_t*。gcc和clang都使用-Wall -WExtra -Wpedantic编译代码片段,并且只报告未使用的变量SOURIC_VICER_TABLE。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    确实,const_cast仅用于将CV合格类型T转换为不同的CV合格类型T。但是,根据标准,符号interrutVectors []的类型和typedef isr_t的类型表示不同的类型, 从而使从一个到另一个的任何const_cast都是非法的。

    根据C+03标准的7.5 1,具有不同语言链接的两个名称是不同的类型,即使它们在其他方面是相同的。

    我们的编译器强制执行此要求,而gcc和clang都不执行。 以下代码根据需要工作:

    extern "C" void (*const interruptvectors[])();
    
    extern "C"{
    typedef void (*ISR_t)(void);
    }
    
    static ISR_t* source_vector_table = const_cast<ISR_t*>(interruptvectors); 

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    啊,我明白了。 不知道,感谢您的澄清!