简体   繁体   English

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

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

I have two versions of Error structure in my library, so I want to use inline namespaces for the versioning. 我的库中有两个版本的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;
        };
    }
}

Here's the sample that illustrates the compilation error that I receive with Visual Studio 2017. Both clang and gcc work fine. 下面的示例演示了我使用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;
}

Looks like a bug in MSVS or maybe I am missing something. 看起来像MSVS中的错误或者我错过了一些东西。 This code works fine without any issues on MSVS: 此代码在MSVS上没有任何问题,工作正常:

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

Any idea why do I receive this error? 知道为什么我会收到这个错误吗?

There is already a bug filed on this issue for VS2017 version 15.9 titled: Inline namespace name not found . VS2017版本15.9已针对此问题提交了一个错误,标题为: 未找到内联命名空间名称

The workaround suggested in the bug report is to specify the namespace as well in function parameter (for eg void core::Foo::someMethod(core::Error err) ) . 错误报告中建议的解决方法是在函数参数中指定命名空间(例如, void core::Foo::someMethod(core::Error err) )。

The final comment on the bug report states that they have fixed the problem in an upcoming release. 关于错误报告的最终评论表明他们已经在即将发布的版本中修复了问题。 (Release version not mentioned). (未提及发行版)。

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

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