c++ - BOOST_FOREACH 宏强制方法脱离 Visual Studio 命名空间

标签 c++ visual-studio boost foreach namespaces

当我在 C++ 方法中使用 BOOST_FOREACH 宏时,不再自动检测到实现,因为该方法被强制从 Visual Studio 确定的相应命名空间(也许是 Intellisense?)。编译没有问题。这里发生了什么?

.h

class testClass
{
public:
    testClass();
    ~testClass();

    void print();
};

.cpp
#include "testClass.h"
#include "stdafx.h"
#include <boost/foreach.hpp> // BOOST_FOREACH
#include <iostream> //std::cout, endl

testClass::testClass()
{
}

testClass::~testClass()
{
}

void testClass::print()
{
    int nums[] = { 1, 2, 3, 4, 5 };
    BOOST_FOREACH(const int a, nums)
    {
        std::cout << a << std::endl;
    }
}

最佳答案

@sehe My question is why the boost macro causes this behaviour, that is the failure to detect the method's implementation in the .cpp – Matthew Green 10 mins ago



答案是:因为宏是预处理器功能。完全实现预处理在实现复杂性和运行时成本方面都是一件昂贵的事情。许多工具在尝试解析 C++ 代码时简化/走捷径。

只有当工具进行完整的预处理并以与编译器完全相同的方式进行解析时才能获得准确的结果¹。如果您对编译器的复杂性有所了解,那么很少有工具会这样做就不足为奇了。

该规则最显着的异常(exception)是 libclang 这确实允许供应商创建“高保真”C++ 工具,而无需实现所有机制。

出于这个原因,我将 YouCompleteMe 与 CMake 的 compiler_commands.json 一起使用确保libclang始终使用相同的标志和定义。它产生了我在 IDE 和工具中见过的最好的完成和诊断。

也就是说,我使用较少的工具(Eclipse 的 C++ 索引、Microsoft 的 Visual Studio Intellisense、QtCreator 的完成²)没有问题。这只是 C++ 领域历史上附带的东西,工具可能并不总是能够理解每个构造。

¹ 并且它必须使用相同的包含路径、标志和定义

² 其他值得注意的是 Doxygen 和 ... SourceInsight(我从未使用过,但在此处进行了分析:How can I make SourceInsight understand smart pointers?)

关于c++ - BOOST_FOREACH 宏强制方法脱离 Visual Studio 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53170543/

相关文章:

c++ - 如何将数据从资源指针存储到 C++ 中的静态内存缓冲区?

c++ - 用于 Objective-C 的 iostream 和 sstream

c++ - 正则表达式分组与 C++ 11 正则表达式库匹配

c++ - 未处理的异常堆栈溢出 win32 控制台应用程序

c++ - 向下转换 boost::polymorphic_pointer_downcast 或 boost::dynamic_pointer_cast 哪个更好用

c++ - boost spirit 气 - 高效报价语法

c++ - boost 无锁使用用户定义类型

c++ - 如何控制 C++ DLL 调试符号加载?

sql - CLR 过程未在 SQL Server 进程中列出

visual-studio - 我不明白 Visual Studio 项目和解决方案的概念