cod*_*101 -1 c++ templates compiler-options c++17
假设我有一个类似的模板类
template<class T> class Foo{
T a;
auto baz(){
return 4.2f;
}
};
int main(){
Foo<int> bar;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否有一个工具可以将这段代码转换为实际的类,并提供如下输出:
class Foo{
int a;
float baz(){
return 4.2f;
}
};
// main goes below this line
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
您会发现它从未发出Foo<int>::baz()想要的,只是因为它从未真正使用过。
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |