如果我有一个功能
int calcStuff_dynamic(const int a, const int b)
Run Code Online (Sandbox Code Playgroud)
和一些模板元代码
template<int a, int b>
struct calcStuff_static {
static const int value = //some more code
};
Run Code Online (Sandbox Code Playgroud)
有没有办法写一个包装器
int calcStuff(const int a, const int b) {
IF_THESE_ARE_KNOWN_CONSTANTS_AT_COMPILE_TIME(a, b)
return calcStuff_static<a, b>::value;
ELSE_TEMPLATE_WOULD_FAIL
return calcStuff_dynamic(a, b);
}
Run Code Online (Sandbox Code Playgroud) c++ templates compile-time-constant compile-time template-meta-programming
./b2 status运行Boost附带的测试的正确方法是什么?要自己测试库吗?
相关问题.运行库测试套件通常是个好主意,还是可以让设计人员在部署之前自己运行它?
我将Java GC测试程序移植到C++(参见下面的代码)以及Python.Java和Python的性能远远超过C++,我认为这是由于new每次都需要完成所有调用才能创建字符串.我尝试过使用Boost,fast_pool_allocator但实际上会将性能从700ms提高到1200ms.我使用分配器是错误的,还是我应该做的其他事情?
编辑:编译g++ -O3 -march=native --std=c++11 garbage.cpp -lboost_system.g ++是版本4.8.1 Python中的一次迭代大约300ms,Java大约50ms.std::allocator给出大约700ms并boost::fast_pool_allocator给出大约1200ms.
#include <string>
#include <vector>
#include <chrono>
#include <list>
#include <iostream>
#include <boost/pool/pool_alloc.hpp>
#include <memory>
//#include <gc/gc_allocator.h>
using namespace std;
#include <sstream>
typedef boost::fast_pool_allocator<char> c_allocator;
//typedef std::allocator<char> c_allocator;
typedef basic_string<char, char_traits<char>, c_allocator> pool_string;
namespace patch {
template <typename T> pool_string to_string(const T& in) {
std::basic_stringstream<char, char_traits<char>, c_allocator> stm;
stm << in;
return stm.str();
}
}
#include "mytime.hpp"
class Garbage {
public: …Run Code Online (Sandbox Code Playgroud) 当我使用以127结尾的范围定义我自己的类型时,编译器不会执行上限检查,这允许变量环绕并在其定义的限制之下变为负数.如果我将范围定义为126,则抛出适当的异常.我在下面列出了程序及其输出.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure GoodType is
type GOOD_TYPE is range -1..126;
package GOOD_TYPE_IO is new Ada.Text_IO.Integer_IO(GOOD_TYPE);
use GOOD_TYPE_IO;
On_Both1 : GOOD_TYPE := 120;
Index : INTEGER := 0;
begin
for Index in 120..130 loop
On_Both1 := On_Both1 + 1;
Put(Index);
Put(": ");
Put(On_Both1);
New_line;
end loop;
end GoodType;
Run Code Online (Sandbox Code Playgroud)
输出:
gnatmake -f goodtype.adb && ./goodtype
120: 121
121: 122
122: 123
123: 124
124: 125
125: 126
raised CONSTRAINT_ERROR : goodtype.adb:16 range check failed
Run Code Online (Sandbox Code Playgroud)
.
with Ada.Text_IO, …Run Code Online (Sandbox Code Playgroud) boost ×2
c++ ×2
ada ×1
compile-time ×1
gnat ×1
linux ×1
memory-pool ×1
templates ×1
testing ×1
unit-testing ×1