cod*_*ack 7 c++ templates boost typedef namespaces
我不知道这是否可行,但这是我想要实现的:在模板化的类中,我想使用模板参数的命名空间.
例如.
template<class P>
class Foo
{
public:
Foo();
virtual ~Foo();
void doSomething(P&);
void doSomethingElse();
protected:
// There I'm hardcoding "namespace1" but that's what I'd like to
// be possibly dynamic
// (I'm assuming template parameter P = namespace1::Type)
void method1(namespace1::Type1&);
...
void methodN(namespace1::TypeN&);
}
// Again, supposing P == namespace1::Type then I want to be using namespace1
// everywhere in the implementation...
using namespace namespace1;
template<class P>
void Foo<P>::doSomething(P& parameter)
{
...
Type1 type1 = P.getType1(); // There namespace1::Type1 is returned !!
method1(type1);
...
}
template<class P>
void Foo<P>::doSomethingElse()
{
...
TypeN typen; // There I want to instanciate a namespace1::TypeN !!
...
}
...
Run Code Online (Sandbox Code Playgroud)
当然我不想专门化模板并为每个可能的P值提供专用的实现,并且我希望避免传递所有类型Type1和TypeN作为模板参数,因为我可能有很多它们.
那可能吗 ?
该项目基于C++ 3,欢迎任何提升解决方案.
更新
作为模板参数P本身就像任何TypeN参数一样,这可能是正确的方法:
template<typename NAMESPACE>
class Foo
{
typedef typename NAMESPACE::Parameter MyParameter;
typedef typename NAMESPACE::Type1 MyType1;
typedef typename NAMESPACE::Type1 MyTypeN;
...
}
Run Code Online (Sandbox Code Playgroud)
是和否.
是的,可以从主要类型中推断出二级类型,通常使用特征系统:
template <typename T> struct Trait { typedef typename T::Secondary Secondary; };
template <typename X>
struct Foo {
typedef typename Trait<X>::Secondary Secondary;
void foo(Secondary const& s);
};
Run Code Online (Sandbox Code Playgroud)
不,你不能推断出命名空间,因此不能使用它; 但请注意如何通过typedef ...在类中使用本地别名()来实现.
| 归档时间: |
|
| 查看次数: |
8741 次 |
| 最近记录: |