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

207 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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