如果给定模板参数,是否有可以将模板 类 转换为实际 类 的 C++ 工具?
Is there a C++ tool which can convert template classes to actual classes if template parameter is given?
假设我有一个模板 class 比如
template<class T> class Foo{
T a;
auto baz(){
return 4.2f;
}
};
int main(){
Foo<int> bar;
return 0;
}
是否有工具可以将此代码转换为实际的 class 并输出为:
class Foo{
int a;
float baz(){
return 4.2f;
}
};
// main goes below this line
一个用推导类型替换所有 auto
和模板参数的工具。
我正在使用模板并且很好奇是否有任何这样的工具可以很好地学习类型推导?
我的意思是,编译器会这样做。您扩展到 Foo
的类型实际上应该称为 Foo<int>
,如果您在调试器中单步执行已编译的程序,您会看到这一点。
虽然我不知道有任何工具可以进行文本扩展,但我很确定我不会喜欢阅读任何重要程序的输出,尤其是使用标准库容器的程序。
编辑 - 好的,这仍然是题外话,但既然我已经回答了,这似乎是相关的:
像这样扩展您的原始代码 (link)
template<class T> class Foo{
T a;
auto baz(){
return 4.2f;
}
};
/* First instantiated from: insights.cpp:9 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
class Foo<int>
{
int a;
inline auto baz();
// inline Foo() noexcept = default;
// inline constexpr Foo(const Foo<int> &) = default;
// inline constexpr Foo(Foo<int> &&) = default;
};
#endif
int main()
{
Foo<int> bar = Foo<int>();
return 0;
}
你会注意到它从来没有发出你想要的Foo<int>::baz()
,只是因为它从未被实际使用过。
假设我有一个模板 class 比如
template<class T> class Foo{
T a;
auto baz(){
return 4.2f;
}
};
int main(){
Foo<int> bar;
return 0;
}
是否有工具可以将此代码转换为实际的 class 并输出为:
class Foo{
int a;
float baz(){
return 4.2f;
}
};
// main goes below this line
一个用推导类型替换所有 auto
和模板参数的工具。
我正在使用模板并且很好奇是否有任何这样的工具可以很好地学习类型推导?
我的意思是,编译器会这样做。您扩展到 Foo
的类型实际上应该称为 Foo<int>
,如果您在调试器中单步执行已编译的程序,您会看到这一点。
虽然我不知道有任何工具可以进行文本扩展,但我很确定我不会喜欢阅读任何重要程序的输出,尤其是使用标准库容器的程序。
编辑 - 好的,这仍然是题外话,但既然我已经回答了,这似乎是相关的:
像这样扩展您的原始代码 (link)
template<class T> class Foo{
T a;
auto baz(){
return 4.2f;
}
};
/* First instantiated from: insights.cpp:9 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
class Foo<int>
{
int a;
inline auto baz();
// inline Foo() noexcept = default;
// inline constexpr Foo(const Foo<int> &) = default;
// inline constexpr Foo(Foo<int> &&) = default;
};
#endif
int main()
{
Foo<int> bar = Foo<int>();
return 0;
}
你会注意到它从来没有发出你想要的Foo<int>::baz()
,只是因为它从未被实际使用过。