有没有办法将浮点数映射到整数或无符号整数,以便除NaN外,保留顺序?
所以如果a和b是浮点数,而F是映射函数,
a <b表示F(a)<F(b),a == b表示F(a)== F(b)
在C++中是否存在严格typedef的习惯用法,可能使用模板?
就像是:
template <class base_type, int N> struct new_type{
base_type p;
explicit new_type(base_type i = base_type()) : p(i) {}
};
typedef new_type<int, __LINE__> x_coordinate;
typedef new_type<int, __LINE__> y_coordinate;
Run Code Online (Sandbox Code Playgroud)
所以我可以做这样的编译时错误:
x_coordinate x(5);
y_coordinate y(6);
x = y; // whoops
Run Code Online (Sandbox Code Playgroud)
将__LINE__在那里看起来可能很麻烦,但我不希望手动创建一组常量仅仅保留每个类型是独一无二的.
这可能是不可能的,但我想知道是否有可能暂时不会超过其原始表达.我有一个指向父对象的对象链,以及一个将创建子对象的成员函数,这里有一个简化的例子
class person{
string name;
person * mommy;
public:
person(const string & nam, person * m = 0) : name(nam), mommy(m) {}
person baby(const string & nam){
return person(nam, this);
}
void talk() const{
if (mommy) mommy->talk();
cout << name << endl;
}
};
int main(){
person("Ann").baby("Susan").baby("Wendy").talk(); // fine
const person & babygirl = person("Julie").baby("Laura"); // not fine
babygirl.talk(); // segfault
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想要使用的方法person是将它传递给一个函数,如下所示:
void use(const person & p) {
p.talk();
}
use(person("Anna").baby("Lisa"));
Run Code Online (Sandbox Code Playgroud)
很好.
只要没有一个临时表现在原始表达式之后,这就可以正常工作,但是如果我把一个最后的临时工具绑定到一个const引用,那么它的父母就无法生存,我得到一个段错误.我可以隐藏person复制构造函数和赋值运算符,但有什么方法可以防止这种错误发生?如果可能的话,我想避免动态分配.
编辑:好吧,我想这是一个糟糕的主意。
是否可以在 C++ 中使用与普通 C++ 引用相同的语义(对于特定类,因为无法重载 . 运算符)进行智能引用,但在 STL 容器中使用时会重新定位?
例如,如果我有一些int_ref重载了普通整数运算符的类,并且构造和赋值如下所示:
class int_ref{
int * p;
public:
int_ref(int * ip) : p(ip) {}
int_ref(const int_ref & other) : p(other.p) {
/* maybe some refcounting stuff here */
}
int_ref & operator = (const int_ref & other){
if (!p)
throw something_bad();
*p = *other.p;
return *this;
}
void reseat(const int_ref & other){
p = other.p;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我不能使用它,std::vector因为它不会重新设置引用,而且我不想要这种事情:
std::vector<int_ref> vec;
int_ref five = new int(5);
vec.push_back(five);
vec.push_back(new …Run Code Online (Sandbox Code Playgroud) 这样做:
union{
int * integer;
char * character;
} u;
u.integer = new int;
delete u.character;
u.integer = new int[5];
delete [] u.character;
Run Code Online (Sandbox Code Playgroud)
我认为如果这些类型中的任何一种具有非平凡的析构函数,这将不起作用,但这样可以吗?
我std::vectors通过在开始时设置容量并使用push_back慢慢填充它们来使用一堆.大多数这些矢量将具有相同的大小(16个元素),尽管有些可能会变大.如果我push_back在最初为0和容量为16的向量上使用16次,我可以确定容量将在16之后正好是16 push_backs吗?
我有一个对字符串进行转换的类,如下所示
class transer{
transer * parent;
protected:
virtual string inner(const string & s) = 0;
public:
string trans(const string & s) {
if (parent)
return parent->trans(inner(s));
else
return inner(s);
}
transer(transer * p) : parent(p) {}
template <class T>
T create() { return T(this); }
template <class T, class A1> // no variadic templates for me
T create(A1 && a1) { return T(this, std::forward(a1)); }
};
Run Code Online (Sandbox Code Playgroud)
所以我可以创建一个子类
class add_count : public transer{
int count;
add_count& operator=(const add_count &);
protected:
virtual …Run Code Online (Sandbox Code Playgroud)