小编her*_*nnk的帖子

使用g ++和specs-file进行预处理

这个问题涉及arm-none-eabi-g ++ 6.2并且与newlib-nano相关联.

当我预处理C源代码-specs=nano.specs,包含newlib.h目录中的文件newlib-nano:

echo '#include <string.h>' |\
/opt/gcc-arm-none-eabi-6_2-2016q4/bin/arm-none-eabi-gcc -specs=nano.specs -x c -E - |\
grep '^# 1 .*newlib\.h'
Run Code Online (Sandbox Code Playgroud)

产出# 1 "/opt/gcc-arm-none-eabi-6_2-2016q4/arm-none-eabi/include/newlib-nano/newlib.h" 1 3 4(如预期).这是因为该文件nano.specs包含(以及其他)行

%rename cpp     nano_cpp
 
*cpp:
-isystem =/include/newlib-nano %(nano_cpp)
Run Code Online (Sandbox Code Playgroud)

但是,如果我通过相同的编译器提供C++源代码

echo '#include <string.h>' |\
/opt/gcc-arm-none-eabi-6_2-2016q4/bin/arm-none-eabi-gcc -specs=nano.specs -x c++ -E - |\
grep '^# 1 .*newlib\.h'
Run Code Online (Sandbox Code Playgroud)

输出读取# 1 "/opt/gcc-arm-none-eabi-6_2-2016q4/arm-none-eabi/include/newlib.h" 1 3.

换句话说:specs-file被忽略.

我知道我应该包含<cstring>而不是<string.h>在C++源代码中,并且GNU g ++通常被调用…/arm-none-eabi-c++而不是,…/arm-none-eabi-gcc -x …

c c++ gcc newlib c-preprocessor

6
推荐指数
1
解决办法
319
查看次数

C++:使用decltype派生函数返回类型

以下是真正的C++ 14应用程序的(过度)简化摘录.出于可维护性的原因,我不想明确指定返回类型foo().我知道C++ 14可以很好地推断出函数的返回类型auto.但真正的应用程序需要函数内部的返回类型.所以我需要它.

以下代码片段编译(g ++ 4.9.2)罚款:

#include <type_traits>
#include <iostream>
#include <string>

//auto foo (std::wstring &s) -> std::remove_reference <decltype (s)>::size_type
auto foo (std::wstring &s) -> decltype (s.length ())
    {
    return s.length ();
    }

int main ()
    {
    std::wstring s {L"hello world"};
    std::cout << foo (s) << "\n";
    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

但是如果我使用函数声明的"注释掉"版本,我会得到以下诊断:

foo.cpp:5:69: error: ‘size_type’ in ‘struct std::remove_reference<std::basic_string<wchar_t>&>’ does not name a type
auto foo (std::wstring &s) -> std::remove_reference <decltype (s)>::size_type
                                                                    ^
Run Code Online (Sandbox Code Playgroud)

怎么了?不是std::wstring::size_type一种类型?不std::remove_reference转换 …

return-type decltype c++11 c++14

2
推荐指数
1
解决办法
694
查看次数

标签 统计

c ×1

c++ ×1

c++11 ×1

c++14 ×1

c-preprocessor ×1

decltype ×1

gcc ×1

newlib ×1

return-type ×1