我在这里是NOOBish,但由于The Heartbleed Bug,我对托管代码非常感兴趣.
我最近在HN读到这句话,其中说:
C和其他没有内存检查的语言不适合编写安全代码.显然不合适.它们需要被限制为编写一个小型核心系统,最好小到可以使用正式(基于证据的)方法进行检查,其余所有应用程序逻辑都应该使用托管代码(例如C#, Java,或其他 - 我没有偏好).
那么Python是一种托管代码语言还是托管代码只是一个Microsoft术语?
来自C++ Draft Standard N3337:
7.1.6.2简单类型说明符
4表示的类型
decltype(e)定义如下:- 如果
e是未加密码的id-expression或未加括号的类成员访问(5.2.5),decltype(e)则是由名为的实体的类型e.如果没有这样的实体,或者e命名一组重载函数,程序就会形成错误;- 否则,如果e是x值,
decltype(e)则是T&&,其中T的类型是e;- 否则,如果e是左值,
decltype(e)则是T&,其中T的类型e;
如果我理解上述内容,
int a;
decltype(a) b = 10; // type of b is int
int* ap;
decltype(*ap) c = 10; // Does not work since type of c is int&
Run Code Online (Sandbox Code Playgroud)
你能解释一下,或者指出一些解释的文件,为什么decltype(*ap)不能只是这样int?
我正在使用 C++ 工作,想要创建一个带有可选参数(QMap)的函数。问题是我该如何设置默认值。我希望它是一张空地图。
void function(int i, QMap< QString, QString > MyMap = ???)
Run Code Online (Sandbox Code Playgroud)
你放什么东西???
我有一个可变的Engine模板类:
template <typename ... Components> class Engine;
Run Code Online (Sandbox Code Playgroud)
我想在编译时为每个组件分配一个数字,这相当于它们的排序.拨打以下电话时会返回此信息:
template <typename Component> int ordinal();
Run Code Online (Sandbox Code Playgroud)
例如,如果:
Engine<PositionComponent, PhysicsComponent, InputComponent> engine;
Run Code Online (Sandbox Code Playgroud)
宣布,电话:
engine.ordinal<PhysicsComponent>();
Run Code Online (Sandbox Code Playgroud)
将返回1并且使用InputComponent而不是PhysicsComponent的类似调用将返回2.
是否可能,如果是的话,怎么会这样呢?
因此,我知道在C ++中,如果静态成员是const文字类型,则可以在类内部对其进行初始化,如下所示:
class test{
public:
static constexpr int stc = 1;
private:
int a = 0;
int b = 0;
int c = 0;
};
Run Code Online (Sandbox Code Playgroud)
静态constexpr变量stc可以用在编译器可以直接替换成员值的地方,即
int main () {int array[test::stc];}
Run Code Online (Sandbox Code Playgroud)
但是,如果在不能由编译器直接替换值的上下文中使用:
int main() { const int &cs = test::stc; }
Run Code Online (Sandbox Code Playgroud)
然后编译器(c)生成一个错误
c++ -std=c++11 -pedantic t.cpp -o t
Undefined symbols for architecture x86_64:
"test::stc", referenced from:
_main in t-a8ee2a.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)
除非静态成员是在类外部定义的,例如:
constexpr int test::stc;
为什么会这样呢?
我们有一个 C++ Web 服务,它会根据每个请求生成子进程。因此,每个孩子都会创建自己的日志文件。我们将 cout 和 cerr 流绑定到一个文件以捕获所有“std::cout”和“std::cerr”。
但是,该服务使用另一个“C”组件来执行一些旧操作。C组件使用“fprintf(stdout,xxx)”和“fprintf(stderr,xxx)”来打印日志。
这些消息不会打印到“cout”和“cerr”日志文件中。相反,它会打印在提示中。我们需要这些 stdout/stderr 消息也转到相应的子日志文件。
有没有一种方法可以将“stdout”和“stderr”也绑定到绑定“cout”和“cerr”的同一文件。
有类似的问题,但没有多语言代码混合示例。
让我知道。
谢谢
以下代码包含的const版本和非const版本operator()。输出
非const运算,假
常量运算,真正的
常量运算,真正的
常量运算,真
即const版本被称为如果任一类型的对象S或是常量如果所提交的指针是常量-线// 2,// 3,// 4。现在,我希望代码// 2可以导致编译时错误,即,我希望const版本只能在const对象上调用。显然,static_assert开启is_const_v<decltype(*this)>不会起作用。还有其他想法吗?
我知道,将非常量变量转换为常量很容易。但这将使滥用至少显而易见。
#include <iostream>
#include <type_traits>
struct S
{
void
operator()( int * )
{
std::cout << std::boolalpha
<< "Non-const op, "
<< std::is_const_v<typename std::remove_reference_t<decltype(*this)> > << '\n';
}
void
operator()( int const * ) const
{
std::cout << std::boolalpha
<< "Const op, "
<< std::is_const_v<typename std::remove_reference_t<decltype(*this)> > << '\n';
}
}; …Run Code Online (Sandbox Code Playgroud) 我有以下程序:
#include <iostream>
namespace detail {
template <class T> char test(int T::*)
{
std::cout << "Came to one\n";
return 0;
}
template <class T> int test(...)
{
std::cout << "Came to two\n";
return 0;
}
}
struct A {};
int main()
{
detail::test<A>(0);
detail::test<int>(0);
}
Run Code Online (Sandbox Code Playgroud)
使用g ++ 4.8.2进行测试时,会产生以下输出:
Came to one Came to two
我的问题:为什么第detail::test一个电话会明确选择第一个版本?
更新
在没有第一个版本的情况下details::test,main编译好的代码.当它存在时,编译器认为它detail::test<A>()比第二个更好.
更新2
在阅读了关于成员的指针之后,即使对于不完整类型或没有指定类型的成员,也是格式良好的.,我尝试了以下,它的工作原理.
struct A;
int main()
{
detail::test<A>(0);
detail::test<int>(0);
}
Run Code Online (Sandbox Code Playgroud)
C++ 11标准有很多地方可以发现我不会想到的概念.
在看到本地引用const可能会延长临时的生命之后,我遇到了有条件地将本地reference-to-const绑定到函数参数或函数调用的临时结果的需要,即:
class Gizmo
{
// Rule of Five members implemented
};
Gizmo Frobnicate(const Gizmo& arg);
void ProcessGizmo(const Gizmo& arg, bool frobnicate)
{
const Foo& local = frobnicate ? Frobnicate(arg) : arg;
// Perform some work on local
}
Run Code Online (Sandbox Code Playgroud)
一个实际的例子:boolean指定是否压缩缓冲区,并且您希望编写以local任一方式操作的统一代码.
上面的例子,但是,在调用小玩意儿的拷贝构造函数arg时frobnicate是false.我设法通过更改Frobnicate(arg)为避免复制构造函数的调用static_cast<const Gizmo&>(Frobnicate(arg)).
我的问题变成:三元运算符如何与关于将本地引用到const绑定到临时的规则进行交互?我的解决方案是否合法且表现良好?
在试图找出如何回答/sf/ask/2352096911/时,我注意到了一个回答的链接一篇相关的SO帖子.我用g ++ 4.8.4尝试了上面链接的答案中的代码,并在程序终止之前得到了分段错误.
这是程序:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream of("cout.txt");
std::cout.rdbuf(of.rdbuf());
std::cout << "test. test. test." << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
构建程序的命令:
g++ -Wall -std=c++11 -g socc.cc -o socc
Run Code Online (Sandbox Code Playgroud)
输出来自gdb:
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the …Run Code Online (Sandbox Code Playgroud)