我正在阅读的STL代码可能很旧......但问题与C++模板语法有关.
问题围绕着这个stl模板函数:
template<class T> std::destroy(T *p) {
p->~T();
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到std :: destroy(T*)函数的特化.所以在我看来,模板函数将为"int"类型实例化相同,并调用"int"的析构函数.为了说明我的观点,我创建了这个模拟std :: destroy的示例代码.我称之为my_destroy,就是这个例子.
#include <iostream>
#include <stdio.h>
using namespace std;
template <class T>
void my_destroy(T * pointer) {
pointer->~T();
}
int main()
{
int *a;
//a->~int(); // !!! This won't compile.
my_destroy<int>(a); // !!! This compiles and runs.
}
Run Code Online (Sandbox Code Playgroud)
}
令我惊讶的是,这行不编译:
a->~int();
Run Code Online (Sandbox Code Playgroud)
但这一行编译:
my_destroy<int>(a);
Run Code Online (Sandbox Code Playgroud)
我的困惑是,我认为这my_destroy<int>(a)将被实例化为相当于a->~int();
对于更大的上下文中的问题,当STL容器<int>擦除元素时,如何std::destroy()工作?
我在编译时看到了这条警告信息(gcc 4.6.3,ubuntu)示例:
struct {
} a;
int main()
{
}
warning: anonymous type with no linkage used to declare variable ‘<anonymous struct> a’ with linkage [enabled by default].
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会不会发出此警告.只有G ++才有.
添加静态清除警告:
static struct {
} a;
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚它意味着什么,特别是为什么type与之相关linkage.我认为链接取决于声明变量的位置和方式,而不取决于变量本身的类型.
我从SO上发布的一些问题中读到了这一段.
我无法弄清楚为什么memcpy不保证非POD类型的安全性.我的理解是,这memcpy只是一个有点明智的副本.
以下是标准报价
对于任何对象(比基类的其他子对象)的
POD类型T,所述对象是否保持类型的有效的值T,构成对象的底层字节(1.7)可以被复制到的阵列char或unsigned char0.41)如果阵列的内容char或unsigned char将被复制回对象,该对象随后应保持其原始值.Run Code Online (Sandbox Code Playgroud)# define N sizeof (T) char buf[N]; T obj ; // obj initialized to its original value std :: memcpy (buf , & obj , N); // between these two calls to std::memcpy, // obj might be modified std :: memcpy (& obj , buf , N); // at this point, each subobject of obj of …
假设我有std::vector V5个元素,
V.erase(V.begin() + 2) 删除第3个元素.
STL vector实现将向上移动第4和第5个元素,然后破坏第5个元素.
即在e中删除元素ivector并不保证调用第i个析构函数.因为std::list,事实并非如此.擦除ith元素调用ith元素的析构函数.
STL对这种行为有什么看法?
这是从我的系统的stl_vector.h获取的代码:
392 iterator erase(iterator __position) {
393 if (__position + 1 != end())
394 copy(__position + 1, _M_finish, __position);
395 --_M_finish;
396 destroy(_M_finish);
397 return __position;
Run Code Online (Sandbox Code Playgroud) 我/usr/include/c++在我的Ubuntu Linux上搜索.在/usr/include/c++/stdexcept,我发现了这个:
class out_of_range : public logic_error
{
public:
explicit out_of_range(const string& __arg);
};
Run Code Online (Sandbox Code Playgroud)
但我无法找到out_of_range()构造函数的定义.
此外,当STL抛出out_of_range()异常时,它使用(例如取自stl_vector.h):
__throw_out_of_range(__N("vector::_M_range_check"));
Run Code Online (Sandbox Code Playgroud)
而且,我唯一能找到的__throw_out_of_range()是:
void __throw_out_of_range(const char*) __attribute__((__noreturn__));
Run Code Online (Sandbox Code Playgroud)
你能和我指出out_of_range班级的定义吗?
假设我有一个类的层次结构:
class Shape {
};
class Circle : public Shape {
}
class Square : public Shape {
}
... hundreds of other shapes continue on...
Run Code Online (Sandbox Code Playgroud)
当给定形状类的名称作为字符串时,我需要实例化该类的对象.
在java中,我可以做这样的事情(伪代码!)
Shape createShape(String name) {
return new Class.forName(name);
}
Run Code Online (Sandbox Code Playgroud)
但是在C++中,我必须这样做:(伪代码!)
Shape * createShape(const string &name) {
if (name.compare("Circle") == 0) {
return new Circle();
}
else if (name.compare("Square") == 0) {
return new Square();
}
else if ... //hundreds of else if continues, one for each shape
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法在C++中处理这样的情况?
如果我通过javascript中的rgba(r,g,b,a)将alpha值设置为1以外的任何值,则浏览器设置的实际值略有不同.但CSS中的值设置完全匹配.
请参阅code -pen-site中的代码示例
<head>
<script type="text/javascript" language="javascript">
window.onload=function() {
document.getElementById("p1").style["background-color"]="rgba(255,0,0,0.3)";
}
</script>
</head>
<body>
<p>RGB colors with opacity:</p>
<p id="p1">Red</p>
<p id="p2">Green</p>
</body>
Run Code Online (Sandbox Code Playgroud)
为什么如果通过Javascript设置CSS颜色,那么数字会改变?
为什么下面的两个表达式返回不同的结果?
Date().valueOf()
"Fri Feb 07 2014 16:03:34 GMT-0500 (Eastern Standard Time)"
new Date().valueOf()
1391807020802
Date().toString()
"Fri Feb 07 2014 16:09:21 GMT-0500 (Eastern Standard Time)"
new Date().toString()
"Fri Feb 07 2014 16:09:26 GMT-0500 (Eastern Standard Time)"
Run Code Online (Sandbox Code Playgroud) 以下代码对于传递编译是合法的.为什么可以将PriorityQueue定义为仅采用可比较的实例元素?
...
PriorityQueue<Object> q = new PriorityQueue<Object>();
q.add(new Object());
...
Run Code Online (Sandbox Code Playgroud)
但它抛出了预期的例外:
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:595)
at java.util.PriorityQueue.siftUp(PriorityQueue.java:591)
at java.util.PriorityQueue.offer(PriorityQueue.java:291)
at java.util.PriorityQueue.add(PriorityQueue.java:268)
at ReentrantLockExample.main(ReentrantLockExample.java:12)
Run Code Online (Sandbox Code Playgroud) 我尝试通过使用宏来嵌入代码块,如下所示:
#define RUN_CODE_SNIPPET(c) do {\
c\
} while(0);
Run Code Online (Sandbox Code Playgroud)
其中'c'是包含在'{}'内的代码块
以下是如何使用它
#include <stdio.h>
#define RUN_CODE_SNIPPET(c) do {\
c\
} while(0);
int main(int argc, char *argv[]) {
RUN_CODE_SNIPPET({
//const char *message = "World";
const char message[] = {'w', 'o', 'r', 'l', 'd', '\0'};
printf("%s\r\n", message);
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里运行它
但是当我使用初始化列表格式时,我收到编译器错误
Run Code Online (Sandbox Code Playgroud)test.c: In function ‘main’: test.c:13:4: error: macro "RUN_CODE_SNIPPET" passed 6 arguments, but takes just 1 }); ^ test.c:9:3: error: ‘RUN_CODE_SNIPPET’ undeclared (first use in this function) RUN_CODE_SNIPPET({ ^~~~~~~~~~~~~~~~ test.c:9:3: …