简体   繁体   中英

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.

#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.

// 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. This code works fine without any issues on 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 .

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) ) .

The final comment on the bug report states that they have fixed the problem in an upcoming release. (Release version not mentioned).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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