例如,有一种方法:
void foo( A* p )
{
auto l = [=](){ /* Do Something using p */ };
// Use l ...
}
Run Code Online (Sandbox Code Playgroud)
我应该如何捕获指针:通过引用或值?里面的lambda p没有改变,只是使用了它的方法.
在自定义类中实现比较器运算符时std::map,我遇到了这个问题,无法看到任何问题.
除了上述问题,还有兴趣简要了解一下,如何operator<工作std::map.
问题的起源:
struct Address {
long m_IPv4Address;
bool isTCP;
bool operator< (const Address&) const; // trouble
};
Run Code Online (Sandbox Code Playgroud) 我正在审查boost单元库,我很困惑为什么boost :: units :: unit类有一个额外的模板参数.这是一个例子:
http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp
template<class Dim,class System, class Enable>
class unit
{
public:
typedef unit<Dim, System> unit_type;
typedef unit<Dim,System> this_type;
typedef Dim dimension_type;
typedef System system_type;
unit() { }
unit(const this_type&) { }
//~unit() { }
this_type& operator=(const this_type&) { return *this; }
// sun will ignore errors resulting from templates
// instantiated in the return type of a function.
// Make sure that we get an error anyway by putting.
// the check in the destructor.
#ifdef …Run Code Online (Sandbox Code Playgroud) c++ templates generic-programming boost-units template-classes
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class A
{
public:
void doStuff(function<void (const string *)> func) const
{
cout << "Const method called" << endl;
for(const auto& i_string : m_vec)
func(i_string);
}
void doStuff(function<void (string *)> func)
{
cout << "Non-const method called" << endl;
doStuff([&func](const string *str)
{
auto mutableString = const_cast<string *>(str);
func(mutableString);
});
}
private:
vector<string *> m_vec;
};
int main()
{
auto a = A{};
a.doStuff([](string *str){
*str = "I modified …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
int __dmasprintf (char **s, const char *format, ...) {
char buf[512];
va_list arg;
int ret;
va_start(arg,format);
ret = vsprintf(buf, format, arg);
va_end(arg);
*s = strdup(buf);
if (*s == NULL) return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想在va_list arg之前的调用中添加一个参数,vsprintf()因为我format在最后包含了1个额外的参数.
如何添加一个参数(例如char * myarg)va_list arg?
或者是否可以通过vsprintf()自定义列表?
#include <random>
int f() {
std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);
return dist(engine);
}
Run Code Online (Sandbox Code Playgroud)
多个线程可以安全地调用此函数吗?功能线程安全吗?这是reduntant调用std::random_device seeder;和std::mt19937 engine(seeder());每一次?
虽然 C++ 标准将虚拟调度的实现留给了编译器,但目前只有 3 个主要编译器(gcc、clang 和 msvc)。
当您通过指向抽象基类的指针调用抽象基类上的方法时,它们如何实现虚拟调度?构建期间如何设置 vtable?
一个简单的“好像”示例会很有用。
c++ polymorphism language-implementation vtable dynamic-dispatch
我有以下模板功能:
template <typename...Args, typename Func>
void call(const char *name, Args...args, Func f)
{
f(3);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它时,就像
call("test", 1, 2, 3, [=](int i) { std::cout<< i; });
Run Code Online (Sandbox Code Playgroud)
编译器抱怨它无法推断模板参数Func.如何解决这个问题,知道args除了函数指针之外的任何类型.
我希望能够避免在条件为false时调用函数,这在编译时已知.现在我使用这样的东西:
template<bool Enabled>
void fun(params)
{
//do nothing
}
template<>
void fun<true>(params)
{
//do something with params.
}
Run Code Online (Sandbox Code Playgroud)
params即使函数体是空的,我也不喜欢这种方法.
我想要一个解决方案,当根本没有调用该函数时,当条件为假时,不会对params进行求值(这可能在第一种情况下使用空函数进行优化,但我不能认为这是真的.编译器).
这甚至可能吗?
c++ ×9
c++11 ×6
templates ×2
boost-units ×1
c ×1
comparator ×1
const ×1
lambda ×1
map ×1
overloading ×1
pointers ×1
polymorphism ×1
static-if ×1
stdthread ×1
variadic ×1
vtable ×1