有一个类似于这个的问题,但没有针对静态,const数据(只读数据)和linux系统的答案.情况如此:系统上的许多程序正在使用共享库.该共享库具有大量的const数据.对于链接到(并使用)共享库的每个进程,是否会在系统内存中复制const数据?我理解(或者我认为)共享库的大小是在"高"级别对其进行计数,但是在 Linux 下, Linux不会交换可执行部分的重复副本.这对静态(名称空间级别)const数据也是如此吗?
我有一些代码我维护我已经开始在clang 3.3下编译.使用"-std = c ++ 11"进行编译时,clang会生成错误(如下所示).我已经将违规代码提炼为以下内容:
#include <stdio.h>
#define DBG_PRT(__format, ...) \
printf("%s:%d:%s: "__format, __FILE__, \
__LINE__, __FUNCTION__, ## __VA_ARGS__)
int main()
{
DBG_PRT("%s\n", "Hi");
}
Run Code Online (Sandbox Code Playgroud)
这是铿锵的输出:
test.cpp:10:5:错误:没有匹配的文字运算符用于调用'operator'"__ format",参数类型为'const char*'和'unsigned int'
Run Code Online (Sandbox Code Playgroud)DBG_PRT("%s\n", "Hi"); ^ test.cpp:4:29: note: expanded from macro 'DBG_PRT' printf("%s:%d:%s: "__format, __FILE__, \ ^ 1 error generated.
如果字符串文字和"__format"之间没有空格,那么预处理器似乎不应该能够扩展__format.但是,当没有指定-std = c ++ 11时,显然是这样.G ++ 4.4.7(有和没有-std = c ++ 0x)编译得很好.
编译器有错误吗?
我将以下内容放入Ideone.com(和codepad.org):
#include <iostream>
#include <string>
#include <tr1/functional>
struct A {
A(const std::string& n) : name_(n) {}
void printit(const std::string& s)
{
std::cout << name_ << " says " << s << std::endl;
}
private:
const std::string name_;
};
int main()
{
A a("Joe");
std::tr1::function<void(const std::string&)> f = std::tr1::bind(&A::printit, &a, _1);
a("Hi");
}
Run Code Online (Sandbox Code Playgroud)
并得到这些错误:
prog.cpp:在函数'int main()'中:
prog.cpp:18:错误:在此范围内未声明'_1'
prog.cpp:19:错误:调用'(A)(const char [3])'不匹配
prog.cpp:18:警告:未使用的变量'f'
我不能为我的生活找出第18行的错误.
在预处理器宏中连接C/C++ 的方法是使用##.stringify的方法是使用#.我正在尝试连接和字符串化.这是从g ++(3.3.2)产生警告
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y) // concat
#define TOKENPASTE3(x, y) TOKENPASTE(#x, #y) // concat-stringify (warnings)
const char* s = TOKENPASTE3(Hi, There)
Run Code Online (Sandbox Code Playgroud)
得到警告是不可接受的
"test_utils/test_registration.h:34:38:警告:粘贴""嗨""和""那里""没有给出有效的预处理令牌"
虽然(使用-E选项)我看到它生成:
const char* s = "Hi""There";
Run Code Online (Sandbox Code Playgroud)
哪个看起来对我来说.
任何帮助将不胜感激.
在c ++中,未指定作为函数调用参数提供的表达式的求值顺序。当我使用std :: apply时,是否可以保证在元组的元素上调用该函数?我有一种情况很重要,那就是该函数首先应用于元组的第一个元素,然后应用于第二个,然后应用于第三个元素...。
作为反例:
template <class Tuple, size_t... Is>
void function(Tuple t, std::index_sequence<Is...>) {
some_func( my_func(get<Is>(t))... );
}
Run Code Online (Sandbox Code Playgroud)
不能保证在元组的每个元素上调用my_func的顺序。
鉴于:
class Hokey
{
public:
explicit C(int i): i_(i) { }
template<typename T>
T& render(T& t) { t = static_cast<T>(i_); return t; }
private:
unsigned i_;
};
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
Hokey h(1);
string s;
h.render(s);
Run Code Online (Sandbox Code Playgroud)
t.cpp: In member function 'T& Hokey::render(T&) [with T = std::string]':
t.cpp:21: instantiated from here
Line 11: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(unsigned int&)'
Run Code Online (Sandbox Code Playgroud)
似乎应该说没有Hokey::render可比的.
当然,如果我提供有效的过载,一切正常.但是根据下面的代码,你再次取消注释行,键盘扼流圈:
string& render(string& s) const {
ostringstream out; …Run Code Online (Sandbox Code Playgroud)