我想知道 ELF 格式是否已指定每个部分应与 4kb 边界对齐?或者,仅在 x86 平台上,ELF 的“实现”应将每个部分与 4kb 边界对齐。
是否有任何规范可以对此进行判断?
首先,我有一个类的头文件,一个没有定义的专业声明(来自互联网的代码示例)
$ cat foo.h
template<typename T>
class foo{
public:
static void init(){
return;
}
};
template<> void foo<int>::init();
Run Code Online (Sandbox Code Playgroud)
然后有2个用于模板专业化的实现文件
$ cat foo_int.cpp
#include "foo.h"
#include<stdio.h>
template<>
void foo<int>::init(){
printf("init int foo\n");
}
$ cat foo_float.cpp
#include "foo.h"
#include<stdio.h>
template<>
void foo<float>::init(){
printf("init float foo\n");
}
Run Code Online (Sandbox Code Playgroud)
终于我有了一个主文件
$ cat main.cpp
#include "foo.h"
int main(){
foo<int>::init();
foo<float>::init();
}
Run Code Online (Sandbox Code Playgroud)
如果我不进行优化就编译并运行它,它将得到:
g ++ foo_int.cpp foo_float.cpp main.cpp && a.out
init int foo
init浮点foo
如果添加优化,则结果将不同:
$ g ++ foo_int.cpp foo_float.cpp main.cpp -O2 && a.out
初始化int foo …
做了一个简单的测试,发现"= default"只适用于特殊的成员函数,如下所示:
#include<cstdio>
#include<utility>
struct Base{
Base(int){printf("Base(int)\n");}
};
struct Derived{
Derived(int)=default;
};
int main(){
Derived d(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
clang将报告编译错误:
error: only special member functions may be defaulted
Run Code Online (Sandbox Code Playgroud)
所以如果只允许"特殊成员函数",这个"= default"似乎没用:因为如果我不在"Derived"中给出特殊成员函数的定义,编译器会为我生成一个,等于使用"=默认".
所以我的问题是,为什么以及何时需要"=默认"?
在STL算法中,有几个“设置”操作,例如set_union、set_difference、set_intersection、set_symmetry_difference\xef\xbc\x8c,但我希望在2个向量之间执行:
\n\nvector<int> A={1,2,3,4};\nvector<int> B={1,3};\nRun Code Online (Sandbox Code Playgroud)\n\n我希望得到AB,所以AB={2,4}。
\n\n有没有一种方便的方法来执行此“减”操作?\n谢谢!
\n我在下面有一个快速测试:
#include<iostream>
using namespace std;
int main(){
int i=2;
auto f=[=]()mutable{++i;};
f();
f();
cout<<i<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但结果仍然打印"2".为什么我没有在可变lambda中修改?我正在使用clang --std = c ++ 1z.
谢谢!
我正在尝试测试 EXPECT_THROW,如下所示。
#include <gtest/gtest.h>
int Foo(int a, int b){
if (a == 0 || b == 0){
throw "don't do that";
}
int c = a % b;
if (c == 0)
return b;
return Foo(b, c);
}
TEST(FooTest, Throw2){
EXPECT_THROW(Foo(0,0), char*);
}
int main(int argc, char* argv[]){
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)
我预计“Throw2”应该成功。但它给出了这个错误信息:
Expected: Foo(0,0) throws an exception of type char*.
Actual: it throws a different type.
Run Code Online (Sandbox Code Playgroud)
那么这里抛出的类型是什么?
我写了一些像这样的代码:
unordered_map<int, int> uii;
uii.insert(make_pair(12,4));
uii.insert(make_pair(3,2));
uii.insert(make_pair(6,1));
uii.insert(make_pair(16,9));
....
Run Code Online (Sandbox Code Playgroud)
当我使用for循环访问此地图时,它按照我插入的正确顺序打印键.我测试了unordered_set,结果相同.
所以我的问题是,C++标准是否保证访问顺序为插入顺序,就像Java一样LinkedHashMap?
我来自C ++,使用C#作为新手,只是尝试了一下:
class Class1
{
int mI = 0;
string mS = "ab";
public static Class1 operator + (Class1 obj1, Class1 obj2)
{
return new Class1()
{
mI = obj1.mI + obj2.mI,
mS = obj1.mS + obj2.mS
};
}
public static void operator += (Class1 obj1)
{
mI += obj1.mI;
mS += obj1.mS;
}
}
Run Code Online (Sandbox Code Playgroud)
我发现该operator+=函数无法编译,并说:
错误CS1019:应重载一元运算符。
那么C#根本不执行这种运算符重载吗?
使用相同的模板声明,是否可以区分具有相同名称、相同参数列表但返回类型不同的两个函数?
template <class T>
int f()...
template <class T>
short f()...
Run Code Online (Sandbox Code Playgroud)
或者,需要一些特殊的代码来实现这一点?
谢谢。
我正在使用 VS2019 并在项目设置中启用了可为空检查语义。我正在尝试使用程序集获取可执行文件的路径,如下所示:
var assembly = Assembly.GetEntryAssembly();
if (assembly == null)
{
throw new Exception("cannot find exe assembly");
}
var location = new Uri(assembly.GetName().CodeBase);//doesn't compile.
Run Code Online (Sandbox Code Playgroud)
它说,“assembly”是一个 [Assembly?] 类型,而 Uri ctor 需要一个字符串,编译错误是:
error CS8602: Dereference of a possibly null reference.
Run Code Online (Sandbox Code Playgroud)
如何修复我的代码以使其编译?非常感谢。