Following code works fine:
以下代码工作正常:
template<typename T>
struct Wrap {};
template<template <class> class OUT, typename IN>
void Extract (OUT<IN> *obj)
{ /* only class 'IN' is used in some way */ }
int main ()
{
Wrap<int> obj;
Extract(&obj);
}
But, I am passing a pointer argument to extract the outer type and inner type.
但是,我传递一个指针参数来提取外部类型和内部类型。
Is there any better way by which I can invoke the method with explicit template instantiation ? Extract<Wrap<int> > ()
;
有没有更好的方法可以通过显式模板实例化来调用该方法?提取
Edit:
I will detail my question a bit more. This explains, why the easy answer like Extract<Wrap, int>();
is not possible. I am writing a text parser, for C++ code. Wherever, parser finds,
我会详细说明我的问题。这解释了为什么像Extract
x = (Type) y;
it should convert into,
它应该转换成,
x = Extract<Type> (y);
Now, the Type can be
现在,Type可以
- any normal type like,
int*
orA**
- some templatized template like
Wrap<A>
任何正常类型,如int *或A **
Now, Extract()
works different for both the cases. I have to figure out using template that, whether it's Extract<int*>
or Extract<Wrap<int> >
.
现在,Extract()对两种情况都有不同的作用。我必须弄清楚使用模板,无论是Extract
==> In simpler language, the method can be called:
==>在更简单的语言中,可以调用该方法:
Extract<int*>()
Extract<Wrap<int> >()
I can figure out if it's called in 1st case, but how can I figure out if it's called in 2nd case ? (provided that, I want to know internal type also).
我可以弄清楚它是否在第一种情况下被调用,但是如何判断它是否在第二种情况下被调用? (前提是,我也想知道内部类型)。
2 个解决方案
#1
0
How about Extract<Wrap, int> (&obj);
?
如何提取
But why do you want to specify it explicitly? Most of the time you'd rather have the compiler deduce the types for you automatically for simplicity.
但是为什么要明确指定它呢?大多数情况下,为了简单起见,您宁愿让编译器为您自动推断出类型。
EDIT: Have you considered just specializing for Wrap<U>
?
编辑:您是否考虑过专攻Wrap?U?
#include <iostream>
template<typename T>
struct Wrap {};
template <class T>
struct Foo
{
static void Extract()
{
std::cout << "Base type" << std::endl;
// Stuff
}
};
template <class U>
struct Foo<Wrap<U> >
{
static void Extract()
{
std::cout << "Extract<Wrap<U> >" << std::endl;
// Stuff for Wrap
}
};
int main ()
{
Foo<int>::Extract();
Foo<Wrap<int> >::Extract ();
}
If needed you can add a specialization for T*
as well.
如果需要,您也可以为T *添加专业化。
#2
0
This works fine
这很好用
template<typename T>
struct Wrap {};
template<template <class> class OUT, typename IN>
void Extract ()
{
IN x; // IN => int
}
int main ()
{
Wrap<int> obj;
Extract<Wrap, int>();
}
You certainly lack basic understanding of C++ templates. Read the book C++ Templates - The Complete Guide.
你当然缺乏对C ++模板的基本理解。阅读“C ++模板 - 完整指南”一书。
#1
0
How about Extract<Wrap, int> (&obj);
?
如何提取
But why do you want to specify it explicitly? Most of the time you'd rather have the compiler deduce the types for you automatically for simplicity.
但是为什么要明确指定它呢?大多数情况下,为了简单起见,您宁愿让编译器为您自动推断出类型。
EDIT: Have you considered just specializing for Wrap<U>
?
编辑:您是否考虑过专攻Wrap?U?
#include <iostream>
template<typename T>
struct Wrap {};
template <class T>
struct Foo
{
static void Extract()
{
std::cout << "Base type" << std::endl;
// Stuff
}
};
template <class U>
struct Foo<Wrap<U> >
{
static void Extract()
{
std::cout << "Extract<Wrap<U> >" << std::endl;
// Stuff for Wrap
}
};
int main ()
{
Foo<int>::Extract();
Foo<Wrap<int> >::Extract ();
}
If needed you can add a specialization for T*
as well.
如果需要,您也可以为T *添加专业化。
#2
0
This works fine
这很好用
template<typename T>
struct Wrap {};
template<template <class> class OUT, typename IN>
void Extract ()
{
IN x; // IN => int
}
int main ()
{
Wrap<int> obj;
Extract<Wrap, int>();
}
You certainly lack basic understanding of C++ templates. Read the book C++ Templates - The Complete Guide.
你当然缺乏对C ++模板的基本理解。阅读“C ++模板 - 完整指南”一书。