繁体   English   中英

在cpp文件中使用来自内联命名空间的类型在MSVS中不起作用

[英]Using type from inline namespace in cpp file does not work in MSVS

我的库中有两个版本的Error结构,所以我想使用内联命名空间进行版本控制。

#pragma once
#include <string>

namespace core {
    inline namespace v2 {
        struct Error {     // <-- new version
            int code;
            std::string description;
        };
    }
    namespace v1 {
        struct Error {     // <-- old version
            int code;
        };
    }
}

下面的示例演示了我使用Visual Studio 2017收到的编译错误.clang和gcc都可以正常工作。

// foo.h
#pragma once
#include "error.h"

namespace core {
    class Foo
    {
    public:
        Foo() = default;
        ~Foo() = default;
        void someMethod(Error err);
    };
}

// foo.cpp
#include "foo.h"
#include <iostream>

void core::Foo::someMethod(Error err) {  // error C2065: 'Error': undeclared identifier
    std::cout << err.code << std::endl;
}

看起来像MSVS中的错误或者我错过了一些东西。 此代码在MSVS上没有任何问题,工作正常:

void core::Foo::someMethod() {    // <-- Error is not passed here
    Error err;
    err.code = 42;
    std::cout << err.code << std::endl;
}

知道为什么我会收到这个错误吗?

VS2017版本15.9已针对此问题提交了一个错误,标题为: 未找到内联命名空间名称

错误报告中建议的解决方法是在函数参数中指定命名空间(例如, void core::Foo::someMethod(core::Error err) )。

关于错误报告的最终评论表明他们已经在即将发布的版本中修复了问题。 (未提及发行版)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM