假设我精心设计了一组类来抽象一些东西,现在我担心我的C++编译器是否能够剥离这些包装并发出非常干净,简洁和快速的代码.我如何找出编译器决定做什么?
我知道的唯一方法是检查拆卸.这适用于简单的代码,但有两个缺点 - 当编译器再次编译相同的代码时,编译器可能会有所不同,而且机器代码分析也不是很简单,因此需要付出努力.
我怎样才能找到编译器如何决定实现我用C++编写的代码?
Eee ......我甚至都不知道我所称的这个名字是否正确......但我想知道是否有一个C++模板跟踪器在那里.它的功能类似于GCC编译器的-E
开关(扩展所有宏),唯一的区别是它会为模板显示相同的东西,例如创建了哪些类,以及部分特化,源代码,模板化方法称为模板参数推导等...
是否有工具将C++中的源代码转换为C/C++中的源代码,但是使用实例化(展开)模板?这对于明确理解C++模板转换的代码是必要的.可能它存在于IDE(MSVS,QtCreator,...)或编译器(ICC,GCC,MSVC,Clang)中?
我的代码是:
#include <iostream>
using namespace std;
template <typename T, int X>
class Test
{
private:
T container[X];
public:
void printSize();
};
template <typename T, int X>
void Test<T,X>::printSize()
{
cout <<"Container Size = "<<X <<endl;
}
int main()
{
cout << "Hello World!" << endl;
Test<int, 20> t;
Test<int, 30> t1;
t.printSize();
t1.printSize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
题:
<int, 20>
用于<int, 30>
.如果我的理解错了,请纠正吗?遗憾的是,我无法使用 C++ 中的任何stl / std库,因为我正在为嵌入式操作系统进行编程,该操作系统仅具有可用的gcc和 4.4.4
裸 C++,因此,没有std::tuple
、std::forward
或。std::apply
std::anything_else
为了帮助理解元通用生成的代码,我提供了一个用clang编译的最小示例代码,因为它可以选择向我们显示生成的模板元编程/元编程代码。
这个问题只是出于好奇,因为我可以按照正确的顺序创建它,而不是以错误的顺序生成整数参数包。这是我用来以错误的顺序生成整数打包程序包的方法:
template<int ...>
struct MetaSequenceOfIntegers { };
template<int AccumulatedSize, typename Tn, int... GeneratedSequence>
struct GeneratorOfIntegerSequence;
template<int AccumulatedSize, typename Grouper, typename Head, typename... Tail, int... GeneratedSequence>
struct GeneratorOfIntegerSequence< AccumulatedSize, Grouper( Head, Tail... ), GeneratedSequence... >
{
typedef typename GeneratorOfIntegerSequence
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...
>::type type;
};
template<int AccumulatedSize, typename Grouper, int... GeneratedSequence> …
Run Code Online (Sandbox Code Playgroud) c++ metaprogramming template-meta-programming variadic-templates c++11