我正在尝试使用两阶段模板化别名,但是无法将别名作为构造函数参数接受。有没有办法做到这一点?
以下代码中显示了我要执行的操作的示例:
#include <memory>
template <typename Bar>
class Foo
{
public:
using FooPtr = std::shared_ptr<Foo<Bar> >;
static FooPtr getFooPtr(Bar someBar) { return std::make_shared<Foo<Bar> >(someBar); }
Foo(Bar bar) : _bar(bar) {}
Bar getBar() { return _bar; }
private:
Bar _bar;
};
template <typename Bar>
class Foobar
{
public:
using FB = Foo<Bar>;
Foobar(FB::FooPtr fooPtr)
: _fooPtr(fooPtr)
{
}
FB::FooPtr getFooPtr() { return _fooPtr; }
private:
FB::FooPtr _fooPtr;
};
int main()
{
Foobar myFoobar<int>(f::getFooPtr(4));
return myFoobar.getFooPtr()->getBar();
}
当我用g ++编译时,得到以下信息:
dev@ubuntu:~/test/fsm$ g++ -std=c++1z test.cpp
test.cpp:23:23: error: expected ‘)’ before ‘fooPtr’
Foobar(FB::FooPtr fooPtr)
^
test.cpp:28:5: error: need ‘typename’ before ‘Foobar<Bar>::FB::FooPtr’ because ‘Foobar<Bar>::FB’ is a dependent scope
FB::FooPtr getFooPtr() { return _fooPtr; }
^
test.cpp:31:5: error: need ‘typename’ before ‘Foobar<Bar>::FB::FooPtr’ because ‘Foobar<Bar>::FB’ is a dependent scope
FB::FooPtr _fooPtr;
^
test.cpp: In function ‘int main()’:
test.cpp:36:11: error: missing template arguments before ‘myFoobar’
Foobar myFoobar<int>(f::getFooPtr(4));
^
test.cpp:37:11: error: ‘myFoobar’ was not declared in this scope
return myFoobar.getFooPtr()->getBar();
编译器是否无法识别Foobar构造函数中的别名FB?还是将Foo :: FooPtr解析为一个类型有问题吗?
谢谢!