这个问题在访谈中: 这段代码是否会导致编译/链接错误,为什么会这样?
template <int T> void f();
template <> void f<0>() {}
void test()
{
f<1>();
}
Run Code Online (Sandbox Code Playgroud)
请解释一下这种行为.非常感谢.
最近我参加了一次C++技术访谈:在那次采访中我问了一个我无法回答的问题:即使我尝试上网和一些论坛但无法得到答案,请参阅下面的代码片段:
using namespace std;
class Base1
{
public:
Base1()
{
cout << "Base1 constructor..." << endl;
}
~Base1()
{
cout << "Base1 Destructor..." << endl;
}
};
class Base2
{
public:
Base2()
{
cout << "Base2 constructor..." << endl;
}
~Base2()
{
cout << "Base2 Destructor..." << endl;
}
};
class Derived : public Base1, public Base2
{
public:
Derived()
{
cout << "Derived constructor...." << endl;
}
~Derived()
{
cout << "Derived Destructor..." << endl;
}
};
int main() …Run Code Online (Sandbox Code Playgroud) 在linux/kernel/signal.c中找到了这个
switch (_NSIG_WORDS) {
default:
for (i = 1; i < _NSIG_WORDS; ++i) {
x = *++s &~ *++m;
if (!x)
continue;
sig = ffz(~x) + i*_NSIG_BPW + 1;
break;
}
break;
case 2:
x = s[1] &~ m[1];
if (!x)
break;
sig = ffz(~x) + _NSIG_BPW + 1;
break;
case 1:
/* Nothing to do */
break;
}
Run Code Online (Sandbox Code Playgroud)
也许这不是一个很好的例子,但我无法理解它是如何工作的,是什么促使Linus将默认部分放在switch语句的前面.
我是系统软件系的学生.现在我正在为Windows开发一个内存管理器.这是我的简单实现malloc()和free():
HANDLE heap = HeapCreate(0, 0, 0);
void* hmalloc(size_t size)
{
return HeapAlloc(heap, 0, size);
}
void hfree(void* memory)
{
HeapFree(heap, 0, memory);
}
int main()
{
int* ptr1 = (int*)hmalloc(100*sizeof(int));
int* ptr2 = (int*)hmalloc(100*sizeof(int));
int* ptr3 = (int*)hmalloc(100*sizeof(int));
hfree(ptr2);
hfree(ptr3);
hfree(ptr1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.但我无法理解是否有理由使用多个堆?好吧,我可以在堆中分配内存并将地址获取到已分配的内存块.但在这里我使用ONE堆.是否有理由使用多个堆?也许对于多线程/多进程应用程序?请解释.
我在 std::vector::push_back() 实现中发现了这个:
void push_back(_Ty&& _Val)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
这在 std::map operator[] 实现中:
mapped_type& operator[](key_type&& _Keyval)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
为什么 _Val 和 _Keyval 是逐个引用的?逐个引用的论证是如何工作的?与参考文献相比,这种方法有什么好处?
PS 这不是逻辑“与”,我清楚地理解这一点。
我有一个ServiceProvider类,其中包含几个指向不同服务的指针,如:
class ServiceProvider()
{
Service3* GetService3();
public:
void Process(Object* o);
void Shrink();
private:
TAutoSpawningPtr<Service1> service1;
TAutoSpawningPtr<Service2> service2;
Service3* service3;
}
Run Code Online (Sandbox Code Playgroud)
注意,这TAutoSpawningPtr是一个理论上的智能指针?lass我正在寻找,而service3被声明为一个普通的指针,以明确显示我需要的行为.身体Process():
void ServiceProvider::Process(Object* o)
{
service1->Process(o);
service2->Process(o);
GetService3()->Process(o);
}
Run Code Online (Sandbox Code Playgroud)
身体GetService3():
void ServiceProvider::GetService3()
{
if(!service3)
{
service3 = new Service3();
}
return service3;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,Service3正在创建一个懒惰的实例,并且在需要之前它不存在.
Shrink()定期调用方法以删除所有内部服务.像这样:
void ServiceProvider::Shrink()
{
service1.Release(); // delete its internal Service1 pointer if it exists.
service2.Release(); // delete its internal Service2 pointer if it exists.
if …Run Code Online (Sandbox Code Playgroud) 我想以原子方式升级我的参考。例如使用compareAndSet,getAndSet和其他原子操作。
我来自 C++,所以在 C++ 中我有volatile关键字和不同的原子内在函数,或者<atomic> API。在java中,还有一个volatile关键字和不同的不安全原子操作。
顺便说一下,还有一个文档齐全的AtomicReference(以及Long, Integer, Boolean),因此JDK创建者为我们提供了一种安全地对引用和原语执行原子操作的方法。API 没有任何问题,它很丰富,而且看起来很熟悉。
但是,还有一个AtomicReferenceFieldUpdater wick 提供了一种执行原子操作的奇怪方式:您必须通过名称通过反射“找到”字段,然后您可以使用完全相同的操作。
所以我的问题是:
AtomicReferenceFieldUpdater什么?在我看来,这是隐含的并且有点奇怪:您需要声明一个 volatile 变量字段AND和字段更新程序本身。那么我应该在哪里使用FieldUpdater?FieldUpdater,而不是变量,这令人困惑。AtomicReferenceFieldUpdater和
AtomicReference被委托给不安全,所以其性能基本相近,但无论如何:是否有任何性能损失
FieldUpdater安剑铮,卓杰参考?我正在尝试使用QtCreator中的te WinAPI编译一个简单的类似helloworld的非Qt C++应用程序.这是代码:
#include <windows.h>
int main()
{
HWND cons = GetConsoleWindow();
SetWindowText(cons, L"I am the console window");
MessageBox(cons, L"Hello world!", L"I am the MessageBox", MB_OK | MB_ICONERROR);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看起来很简单,但是当我尝试构建它时,编译失败了:
main.obj:-1: error: LNK2019: unresolved external symbol __imp__MessageBoxW@16 referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol __imp__SetWindowTextW@8 referenced in function _main
Run Code Online (Sandbox Code Playgroud)
我开始寻找,我找到了这个,但它根本没有帮助我,因为当我写下这个:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib"
Run Code Online (Sandbox Code Playgroud)
甚至这个:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\shell32.lib"
Run Code Online (Sandbox Code Playgroud)
在我看来.pro,这些"符号"仍然没有得到解决.我跑后qmake的每一个变化的.pro-file内容.那么,有什么想法吗?
我有两个类A和B,都定义了foo()具有共同签名的方法(不接受任何内容,返回无效)。它们没有声明此方法的公共基类(或接口)。我想调用这个方法,不管是 As 还是 B,只要他们能响应这个调用。这种方法称为Duck Typing。
我知道有一个指令叫做invokedynamic:
调用动态指令的每个实例称为动态调用点。动态调用站点最初处于未链接状态,这意味着没有为调用站点指定要调用的方法。如前所述,动态调用站点通过引导方法链接到方法。动态调用站点的引导方法是编译器为动态类型语言指定的方法,JVM 调用一次以链接站点。从引导方法返回的对象永久确定调用站点的行为。
所以我尝试使用MethodHandles来实现这一点。这是示例:
public static class A {
public void foo() {
}
}
public static class B {
public void foo() {
}
}
public static void main(String[] args) throws Throwable {
final MethodHandle foo = MethodHandles.lookup()
.findVirtual(A.class, "foo", MethodType.methodType(void.class));
foo.invoke(new B());
}
Run Code Online (Sandbox Code Playgroud)
当然,我有:
Exception in thread "main" java.lang.ClassCastException: Cannot cast Main$B to Main$A
at sun.invoke.util.ValueConversions.newClassCastException(ValueConversions.java:461)
at sun.invoke.util.ValueConversions.castReference(ValueConversions.java:456)
at …Run Code Online (Sandbox Code Playgroud) 此代码取自教程:
def main():
stri = "Hello, {person}"
stri.format(person="James")
print(stri) #prints "Hello, {person}"
Run Code Online (Sandbox Code Playgroud)
为什么format()不工作?
c++ ×6
java ×2
reference ×2
winapi ×2
annotations ×1
atomic ×1
c ×1
duck-typing ×1
format ×1
inheritance ×1
map ×1
methodhandle ×1
pointers ×1
python ×1
qt ×1
qt-creator ×1
reflection ×1
stl ×1
string ×1
templates ×1
vector ×1
windows ×1