标签: default-arguments

Lambda函数作为构造函数中std :: function的默认参数

我想在类的构造函数中为functor参数设置一个默认仿函数.作为一个最小的例子,我提出了一个应该作为过滤器服务器的类,它过滤Tiif 类型的元素,过滤器函数返回true.过滤器函数应该在构造函数中提供,默认为"accept all"过滤器函数:

template<class T>
class Filter
{
public:
    typedef std::function<bool(const T&)> FilterFunc;

    Filter(const FilterFunc & f = [](const T&){ return true; }) :
        f(f)
    {
    }

private:
    FilterFunc f;
};
Run Code Online (Sandbox Code Playgroud)

我实例化模板类,如下所示:

int main() {
    Filter<int> someInstance;  // No filter function provided    (<-- line 19)
}
Run Code Online (Sandbox Code Playgroud)

但是,gcc 4.7似乎不喜欢这段代码:

prog.cpp: In constructor ‘Filter<T>::Filter(const FilterFunc&) [with T = int; Filter<T>::FilterFunc = std::function<bool(const int&)>]’:
prog.cpp:19:17: internal compiler error: in tsubst_copy, at cp/pt.c:12141
Please submit a full bug report,
with …
Run Code Online (Sandbox Code Playgroud)

c++ lambda default-arguments c++11

5
推荐指数
1
解决办法
2409
查看次数

参数的默认值基于其他参数

我想要实现这样一个类:

class A{
    int a;
    int b;
    int c;
    A():a(),b(),c(){};
    A(int ia,int ib,int ic=ia+ib):a(ia),b(ib),c(ic){};  //this is what i need
};
Run Code Online (Sandbox Code Playgroud)

我希望ic的默认值是基于ia和ib计算的,这里的代码在编译时会出错.

我想知道是否有办法得到这样的东西.

谢谢.

c++ constructor default-arguments c++11 delegating-constructor

5
推荐指数
1
解决办法
122
查看次数

为什么允许这些默认参数?

我发现了这个问题,我完全感到困惑.

答案说b无效,"非静态成员不能用作默认参数." 这很有道理.

我不明白的是为什么其他两个都没问题.事实上,如果默认值不是常量表达式,我很难理解语义是什么......

这里发生了什么?在编译时清楚地评估默认参数.编译器只是选择当前值吗?

#include <iostream>

int g_x = 44; 

struct Foo 
{ 
  int m_x; 
  static int s_x; 

  Foo(int x) : m_x(x) {} 
  int a(int x = g_x) { return x + 1; } 
  int b(int x = m_x) { return x + 1; }
  int c(int x = s_x) { return x + 1; }
}; 

int Foo::s_x = 22; 

int main(int argc, char** argv) 
{ 
  Foo f(6); 

  std::cout << f.a() << std::endl; 
  std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ default-arguments

5
推荐指数
2
解决办法
197
查看次数

使用默认参数转发引用?

我在尝试弄清楚如何指定转发引用(以前由 Scott Meyers 称为通用引用)的默认参数时遇到困难。

这是尝试做我想做的事情的代码示例:

struct encoder_t {

} const encoder = {};

struct validator_t {

} const validator = {};

struct test {

    template <typename Range, typename Encoding, typename Validation>
    test ( Range&& range, Encoding&& encoding = encoder, Validation&& validation = validator ) {

    }

};

int main() {

    test( "woof" );

}
Run Code Online (Sandbox Code Playgroud)

也可在 Coliru 上购买

仔细检查这些错误,您发现可以通过默认模板参数来使其工作,然后默认构造参数:

// Works! But the syntax is strange... potential ramifications/deduction mishaps?
// Is this the "proper" way to default these …
Run Code Online (Sandbox Code Playgroud)

c++ templates constructor default-arguments forwarding-reference

5
推荐指数
1
解决办法
587
查看次数

元类默认参数值(Delphi 2009)

我想在metaclass参数中给出一个默认值:

type
  TMyClass = class
  end;

type
  TMyClassMetaClass = class of TMyClass;

procedure MyProcedure(const AMetaClass: TMyClassMetaClass = TMyClass);
Run Code Online (Sandbox Code Playgroud)

有可能的?在Delphi2009中它给出了错误:E2026预期的常量表达式

delphi metaclass default-arguments

5
推荐指数
1
解决办法
80
查看次数

使用std :: enable_if作为函数参数与模板参数之间有什么区别?

我想知道std::enable_if用作函数参数与模板参数之间有什么区别?

我有以下2个功能模板:

#include <type_traits>

template<typename T>
void f_function(T, typename std::enable_if_t<std::is_pod<T>::value, int> = 0)
{
}

template<typename T, typename = typename std::enable_if_t<std::is_pod<T>::value>>
void f_template(T)
{
}

int main()
{
  int x = 1;
  f_function(x);
  f_template(x);
}
Run Code Online (Sandbox Code Playgroud)

产生以下程序集(如https://godbolt.org/g/ON4Rya):

main:
        pushq   %rbp
        movq    %rsp, %rbp
        subq    $16, %rsp
        movl    $1, -4(%rbp)
        movl    -4(%rbp), %eax
        movl    $0, %esi
        movl    %eax, %edi
        call    void f_function<int>(int, std::enable_if<std::is_pod<int>::value, int>::type)
        movl    -4(%rbp), %eax
        movl    %eax, %edi
        call    void f_template<int, void>(int)
        movl    $0, %eax
        leave …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming function-templates default-arguments

5
推荐指数
0
解决办法
879
查看次数

只传递第二个模板参数的方法

所以我有一个模板函数,它有一个默认的第二个参数.它的第一个参数可以推导出来,所以类似于:

template <typename F, typename S = int>
void foo(const F param)
Run Code Online (Sandbox Code Playgroud)

这在一般情况下工作正常,我只是打电话foo(bar).但是在我想要指定第二个参数的情况下,我不能这样做:foo<char>(bar)因为char被视为F.显然情况并非如此,因为bar不是a char,所以F应该是可以推导的.

有没有一种方法可以在这里只传递一个模板参数,这个参数将适用于S,仍然F可以推导出来,并且S在一般情况下仍然是默认的?

c++ templates parameter-passing default-arguments template-argument-deduction

5
推荐指数
1
解决办法
109
查看次数

pybind11 中的命名默认参数

我正在使用 pybind11 将 C++ 类方法包装在转换 lambda“垫片”中(由于原因,我必须这样做)。该方法的参数之一在 C++ 中是默认的。

class A
{
   void meow(Eigen::Matrix4f optMat = Eigen::Matrix4f::Identity());
};
Run Code Online (Sandbox Code Playgroud)

在我的 pybind 代码中,我想保留这个可选参数:

py::class_<A>(m, "A")
       .def(py::init<>())
       .def("meow",
            [](A& self, Eigen::Matrix4f optMat = Eigen::Matrix4f::Identity()) 
            {
               return self.meow( optMat ); 
            });
Run Code Online (Sandbox Code Playgroud)

如何optMat在生成的 Python 代码中创建可选的命名参数?

c++ python default-arguments pybind11

5
推荐指数
1
解决办法
2525
查看次数

以默认参数作为参数的函数

所以,我知道你可以像这样传递一个函数作为参数:

int a(int x) {
    return x + 1;
}

int b(int (*f)(int), int x) {
    return f(x); // returns x + 1
}
Run Code Online (Sandbox Code Playgroud)

我还知道你可以有一个带有默认参数的函数,如下所示:

int a(int x = 1) {
    return x;
}

a(2); // 2
a();  // 1
Run Code Online (Sandbox Code Playgroud)

但是,如何将带有默认参数的函数传递给函数并保留此行为?

我尝试过以下方法:

int a(int x = 1) {
    return x;
}

a(2); // 2
a();  // 1
Run Code Online (Sandbox Code Playgroud)

int b(int (*f)(int), int x) {
    f(x); // works as expected
    f();  // doesn't work because there aren't enough arguments to f …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers function default-arguments

5
推荐指数
1
解决办法
290
查看次数

cppreference.com 中的这段话(默认参数)是什么意思?

来自默认参数的 cppreference 页面:

默认参数中不允许使用非静态类成员(即使未对它们求值),除非用于形成指向成员的指针或在成员访问表达式中:

int b;
class X
{
    int a;
    int mem1(int i = a); // error: non-static member cannot be used
    int mem2(int i = b); // OK: lookup finds X::b, the static member
    static int b;
};
Run Code Online (Sandbox Code Playgroud)

我无法理解“除非用于形成指向成员的指针或在成员访问表达式中”。并且例子中没有给出相关代码。

c++ default-arguments

5
推荐指数
1
解决办法
137
查看次数