【发布时间】:2021-11-06 19:28:33
【问题描述】:
// module A
export module A;
import <type_traits>;
namespace test
{
export{
template<typename T, std::size_t N>
class Foo;
template<typename T>
using Foo1 = Foo<T, 1>;
template<typename T>
struct is_Foo : std::false_type {};
template<typename T, std::size_t N>
struct is_Foo<Foo<T, N>> : std::true_type {};
template<typename T>
constexpr static bool is_Foo_v = is_Foo<T>::value;
template<typename T>
concept Foo_t = is_Foo_v<T>;
}
}
// module B
export module B;
export import module A;
namespace test
{
export{
template<typename T, std::size_t>
class Foo{ ... };
}
}
// module C
export module C;
export import module B;
namespace test
{
export{
// (1)
template<Foo_t T>
constexpr void test_func(const T& t) { ... }
}
}
// test.cpp
import module C;
using namespace test;
constexpr void test_foo()
{
// (2)
constexpr Foo1 f1{1, 2, 3};
// (3)
test_func(f1);
// (4)
constexpr Foo1 f2{4, 5, 6};
}
(1): 概念 Foo_t 在这里很好用
(2):使用undefined class test::Foo<int, 1>
虽然编译器输出的是未定义类的使用,但IDE还是可以提示我们使用Foo1<int> = Foo<int, 1>;
(3):编译器无此行输出,IDE未报错(红色标注)
(4):同(2)
注意:代码全部经过测试,绝对没有语法错误(如果不编译成模块使用hpp,代码运行正常)
可能有人注意到模块导入有顺序要求,我也测试过,不能解决问题。
在test.cpp文件中,即使我再次导入所有模块,也无法解决问题。而且错误还在使用undefined class test::Foo<int, 1>
// test.cpp
import module A;
import module B;
import module C;
// not work
总结是:
-
为什么符号导出失败?
-
更令人费解的是,为什么IDE可以找到我们使用的符号(虽然
jump to definition不能用来跳转),但是编译器却找不到?
如何解决这个问题?
=============================================
经过各种尝试,我似乎已经解决了上述问题(我将多个模块合并为一个模块,并将原来的内容分成多个子模块),但现在又出现了新的问题。
// module Foo
export module Foo;
export{
template<typename T> struct Foo_trait : std::false_type
{
constexpr static auto size = 1;
}
constexpr auto a_func_base_on_specialization_type_Foo_trait(...)
{
...
}
}
// module Bar
export module Bar;
import module Foo;
export
{
template<typename T, std::size_t N>
class Baz;
template<typename T, std::size_t N>
struct Foo_trait<Baz<T, N>> : std::true_type {
constexpr static auto size = N;
}
}
template<typename T, std::size_t N>
class Baz {
constexpr Baz(something) :
data(a_func_base_on_specialization_type_Foo_trait(something)) {}
}
发现不能用a_func_base_on_specialization_type_Foo_trait来构造Baz类,因为大小还是1而不是N
=============================================
我好像找到了上述问题的原因
【问题讨论】:
-
我注意到即使我不使用别名(Foo1)而是直接使用Foo,也会报同样的错误。
标签: c++ module visual-studio-2019 c++20