我正在玩,std::variant, lambdas
并且std::future
当我试图将它们组合在一起时得到超级奇怪的结果.以下是示例:
using variant_t = std::variant<
std::function<std::future<void>(int)>,
std::function<void(int)>
>;
auto f1 = [](int) { return std::async([] { return 1; }); };
auto f2 = [](int) { return std::async([] { }); };
variant_t v1(std::move(f1)); // !!! why DOES this one compile when it SHOULDN'T?
auto idx1 = v1.index(); //equals 1. WHY?
variant_t v2(std::move(f2)); // !!! why DOESN'T this one compile when it SHOULD?
Run Code Online (Sandbox Code Playgroud)
这是编译错误:
错误C2665'std :: variant <std :: function <std :: future <void>(int)>,std :: function <void(int)>> …
我有第三方控制台应用程序.我需要从我的应用程序运行它,但我不能将它作为一个单独的进程运行(因为我需要使用它的依赖项:手动填充导入表,设置挂钩等).所以我可能应该main
手动调用此可执行文件的功能.这是我试图这样做的方式:
auto hMod = LoadLibrary("console_app.exe")
我坚持到最后一步.
以下是我试图调用入口点的方法:
void runMain(HINSTANCE hInst)
{
typedef BOOL(WINAPI *PfnMain)(int, char*[]);
auto imageNtHeaders = ImageNtHeader(hInst);
auto pfnMain = (PfnMain)(DWORD_PTR)(imageNtHeaders->OptionalHeader.AddressOfEntryPoint + (DWORD_PTR)hInst);
char* args[] = { R"(<console_app_path>)", R"(arg1)", R"(arg2)" };
pfnMain(3, args);
}
Run Code Online (Sandbox Code Playgroud)
有用.但它就好像没有争论一样.
我哪里错了?如何在我的进程中使用参数运行可执行文件?谢谢.
我已经调查过我的特定第三方exe如何获取cmd参数并发现:
GetCommandLine
,也不调用它call _initterm
电话argc
和argv
参数进行调查(见下图)
cs:argc
cs:argv
您能解释一下,_initterm
实际存在的CMD参数究竟存在了什么?
c++ program-entry-point systems-programming portable-executable entry-point
我正在实现我自己的类,它提供了其成员的延迟初始化.我this
在lambda中遇到了一种奇怪的捕获行为.
这是一个再现此错误的示例.
//Baz.h
#include <memory>
#include <functional>
#include "Lazy.hpp"
struct Foo
{
std::string str;
Foo() = default;
Foo(std::string str) : str(str) {}
Foo(Foo&& that) : str(that.str) { }
};
class Baz
{
std::string str;
Lazy<std::unique_ptr<Foo>> foo;
public:
Baz() = default;
Baz(const std::string& str) : str(str)
{
//lazy 'this->foo' initialization.
//Is capturing of 'this' valid inside ctors???.
this->foo = { [this] { return buildFoo(); } };
}
Baz(Baz&& that) : foo(std::move(that.foo)), str(that.str) { }
std::string getStr() const
{
return …
Run Code Online (Sandbox Code Playgroud) 我所有的问题都与 vc++ 编译器有关,但我猜其他 c++ 编译器具有相同的行为。
//stdafx.h
#include <boost/variant/variant.hpp>
//test1.cpp
#include "stdafx.h"
#include <boost/variant/variant.hpp>
...
//test2.cpp
#include "stdafx.h"
...
Run Code Online (Sandbox Code Playgroud)