在我的本机C++程序的Visual Studio中,我想在调试期间获得一些变量的图.大多数情况下,我通过编辑autoexp.dat来使用对象的文本表示.但对于某些变量,最好是绘制一个图而不是文本形式的值.
到目前为止,我已经使用了一个函数plot(const void* address,const char* type),并从立即窗口调用它,给出变量地址和类型,并在内部将其转换为正确的类型.
但这种方法有两个缺点:
一般来说,我感兴趣的是像Matlab或Ch IDE这样的东西,我可以在程序处于调试中断时从外部绘制某些变量.
如果没有专门针对每个类模板/类,是否可以编写一个通用的"重新绑定"元函数,以便给出
template<class > struct foo;
struct bar;
Run Code Online (Sandbox Code Playgroud)
下列
is_same<rebind<foo<int>,float>,foo<float>>
is_same<rebind<bar>,bar>
Run Code Online (Sandbox Code Playgroud)
有可能
is_same< rebind<std::vector<int>,float>,std::vector<float>>
Run Code Online (Sandbox Code Playgroud)
返回一个等效的类型为true?
在将我的一些C++ 98代码更新到C++ 11时,我发现统一初始化并不是那么统一.其中一些与不完整类型相关void,而其他与pod有关.例如,对于简单的可复制类型,当初始化涉及复制/移动构造函数时,统一初始化对于直接初始化或复制初始化都不起作用.
例如
template<class T>
T foo() { return T("Hello World");}
foo<void>();
foo<std::string>();
--------
template<class T>
T foo() { return T{"Hello World"};}
foo<void>();
foo<std::string>();
Run Code Online (Sandbox Code Playgroud)
虽然第一部分编译,但后半部分失败了 error: compound literal of non-object type 'void'
struct pod { int x;int y;};
pod p{1,2}; //ok pod p(1,2) is error as usual
pod p2(p);
struct foo
{
foo(foo const& rhs) : p_(rhs.p_){}
pod p_;
};
--------
struct pod { int x;int y;};
pod p{1,2};
pod p2{p};
struct foo
{
foo(foo const& rhs) …Run Code Online (Sandbox Code Playgroud) 在查看C++ 14的元函数别名提议(TransformationTraits Redux,v2,N3655)时,我注意到,不仅类型转换类型(例如add_const),类型到值元函数(例如is_void)也是类型别名.(N3797中没有).
混淆类型对元函数的值是否有任何优势?我认为,可以在没有这些别名的情况下使用它们,例如enable_if_t<is_void<T>::value,T>或enable_if_t<is_void<T>{}(),T>存在转换操作时.(我猜is_void<T>::type::value是一样的is_void<T>::value)
如果类型值元函数需要是别名,不要将它们作为变量模板别名(我没有C++ 14编译器,也从不使用变量模板.所以语法可能是错误的)?例如别名is_void为
template <class T>
constexpr bool is_void_t = is_void<T>::value;
Run Code Online (Sandbox Code Playgroud)
代替
template <class T>
using is_void_t = typename is_void<T>::type;
Run Code Online (Sandbox Code Playgroud)
然后一个人可以在enable_if_t<is_void_t<T>,T>没有助推风格的情况下写作enable_if,并且构图表达将更容易(例如enable_if_t<(is_void_t<T> || is_integral_t<T>),T>
C ++ 11引入emplace了在序列内部就位构造元素的功能。这是对insert元素进行复制或移动的补充。
但是,在的多个重载中insert,只有单个元素插入版本,
即
iterator insert( const_iterator p, T const& x);
iterator insert( const_iterator p, T&& x );
Run Code Online (Sandbox Code Playgroud)
有一个emplace版本,
template< class... Args >
iterator emplace(const_iterator p, Args&&... x);
Run Code Online (Sandbox Code Playgroud)
是否有任何理由不允许n使用来就地构建元素emplace?
虽然像
template< class... Args >
iterator emplace(const_iterator p,size_type n,Args&&... x);
Run Code Online (Sandbox Code Playgroud)
就像相应的 insert
iterator insert(const_iterator p,size_type n,const_reference x);
Run Code Online (Sandbox Code Playgroud)
可能会与其他重载冲突,将构造函数参数用作tuple,或使用一些特殊标记(例如in_place_t可能会消除歧义)。
编辑
建议的功能emplace_n对于vector可能像下面给出的一个行为
template<class... Args>
iterator emplace_n(const_iterator p,size_type n,Args&&... x)
{
size_type const …Run Code Online (Sandbox Code Playgroud) 使用 lombok,我有兴趣通过其构造函数将基类实例中的所有字段复制到派生类,这与 C++ 复制构造函数所做的非常相似。目前关注的不是文案是深还是浅。我有一个基类,如下所示,
class Parent {
.... fields
}
Run Code Online (Sandbox Code Playgroud)
我有兴趣自动生成一个派生类构造函数,它采用基类实例并将所有字段复制(浅或深)到派生的一个。例如
class Child extends Parent {
... derived fields
Child(Parent p) { // can be implemented as super(p);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以根据需要灵活地对Parent和Child类进行注释,但是不想手工制作构造函数,它会一一复制每个字段。示例用法
Parent parent = Parent.of(....);
Child child = new Child(parent);
Run Code Online (Sandbox Code Playgroud) 给定模板别名
template<unsigned U>
using uint_ = integral_constant<unsigned,U>;
Run Code Online (Sandbox Code Playgroud)
部分专业化
template<class T,class P>
struct size{};
Run Code Online (Sandbox Code Playgroud)
如
template <class T,unsigned U>
struct size<T,uint_<U>>{};
Run Code Online (Sandbox Code Playgroud)
template parameter can not be deduced对于clang 3.1 生成警告,而gcc 4.7没有生成警告
那么,代码是否格式错误?
我有一个向量,它包含一些相互分离的浮点值,并根据某些函数进行排序.例如,
double foo(double x)
{
return 199.1*x;
}
double x = 3000000.3157;
double y = x + DBL_EPSILON;
std::vector<double> s { y,y+10};
std::sort(s.begin(),s.end(),[](double x,double y) { return foo(x) < foo(y) ;} );
Run Code Online (Sandbox Code Playgroud)
现在有人有一把钥匙跟我所在的钥匙很近s,例如x.在lambda时代,所有人都有自己的搜索功能,比如
std::cout<<std::distance(s.begin(),std::lower_bound(s.begin(),s.end(),x,
[] (double x,double y) { return foo(x) < foo(y);}))<<std::endl;
std::cout<<std::distance(s.begin(),std::lower_bound(s.begin(),s.end(),x,
[] (double x,double y) { double f1 = foo(x);
double f2 = foo(y);
return f1 < f2;}))<<std::endl;
Run Code Online (Sandbox Code Playgroud)
并获得不同的位置(相应的值非常不同).
在查看用法时,看起来,它们与寻找密钥有关k
r,其中(理想地应该是[0,1])附连到连续值x1&x2使得函数返回值f(x1,x2,r) …