在 cpp 文件中使用内联命名空间的类型在 MSVS 中不起作用
Using type from inline namespace in cpp file does not work in MSVS
我的库中有两个版本的错误结构,所以我想使用内联命名空间进行版本控制。
#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 的这个问题上已经存在一个错误,标题为:Inline namespace name not found。
错误报告中建议的解决方法是在函数参数中指定命名空间(例如 void core::Foo::someMethod(core::Error err)
)。
错误报告的最后评论指出他们已在即将发布的版本中修复了该问题。 (未提及发行版本)。
我的库中有两个版本的错误结构,所以我想使用内联命名空间进行版本控制。
#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 的这个问题上已经存在一个错误,标题为:Inline namespace name not found。
错误报告中建议的解决方法是在函数参数中指定命名空间(例如 void core::Foo::someMethod(core::Error err)
)。
错误报告的最后评论指出他们已在即将发布的版本中修复了该问题。 (未提及发行版本)。