使用func的特定默认参数

时间:2013-04-27 21:11:27

标签: c++

如果我有以下功能定义。在调用时我希望仅为a和c传递值并使用b的默认值我将如何调用此函数

void func(int a=5,int b=2, int c=3)
{
..........
.........
}

5 个答案:

答案 0 :(得分:2)

C ++不支持以下语法:

func(1, , 2);

这意味着只有您省略的最右侧参数才能使用默认值。

如果您希望能够以任意组合使用默认参数,可以考虑使用boost::optional

#include <boost/optional.hpp>
#include <iostream>

typedef boost::optional<int> OptionalInt;
typedef boost::optional<int> Default;

void func(OptionalInt a, OptionalInt b, OptionalInt c)
{
   if (!a) a = 5;
   if (!b) b = 2;
   if (!c) c = 3;
   std::cout << *a << std::endl;
   std::cout << *b << std::endl;
   std::cout << *c << std::endl;
}

int main()
{
   func(1, Default(), 1);
}

输出:

1
2
1

答案 1 :(得分:1)

根据C ++ 11标准(N3485):8.3.6 [dcl.fct.default]

  

对于非模板函数,可以在稍后的同一范围内的函数声明中添加默认参数。   标准的例子:

     

void f(int,int)

     

void f(int,int = 7); // OK

因此规则是您必须从参数列表的右侧指定默认参数。您不仅可以指定a的默认值,而无需在此之前指定bc

void func(int a = 10, int b, int c); //ERROR

但是,您可以尝试以下方法:

void func(int a,int b, int c=3); //set default for c at first
void func(int a,int b = 5, int c);  //at later declarations, set for b
void func(int a =10, int b, int c);//at even later declarations, set for a

使用上述内容,您可以按如下方式调用该函数:

func(20); //call use default value of b and c
func(15,20); //call use default value of c
func(10,20,30); //do not use default value
func(); // use default values for a, b and c

因此,您可以根据需要同时使用bc的默认值,或仅使用c,但无法使用默认值调用func b的值仅因为它是参数列表的中间值,类似地,您不能仅使用默认值func来调用a。这样,您只需添加声明,并且没有冗余代码。但是,您无法真正调用它们,以便它们可以以任意方式使用默认值。

您可以在此处找到实时示例default arguments example

答案 2 :(得分:0)

建议:Boost Parameter

#include <boost/parameter/name.hpp>
#include <boost/parameter/preprocessor.hpp>

BOOST_PARAMETER_NAME(a)    
BOOST_PARAMETER_NAME(b)
BOOST_PARAMETER_NAME(c)

BOOST_PARAMETER_FUNCTION((void),
                         func,
                         tag,
                         (optional            
                          (a, *, 5)
                          (b, *, 2)
                          (c, *, 3)))
{
}
int main()
{
    func(_a = 5, _c = 3);
}

答案 3 :(得分:0)

有几种选择。

选项1:传递类似字典的对象:

struct myParameters {
    int a,b,c;
    myParameters( ) : a(3), b(4), c(5) {}
}
void myFunction( const myParameters &p ) {
    int a = p.a;
    int b = p.b;
    int c = p.c;
    // stuff
}

myParameters t;
t.a = 3;
t.c = 7;
myFunction( myParameters );

选项2:使用其他无意义的值来表示默认值:

#define DEFAULT -1;
void myFunction( int a = DEFAULT, int b = DEFAULT, int c = DEFAULT ) {
    if( a == DEFAULT ) { a = 3; }
    if( b == DEFAULT ) { b = 4; }
    if( c == DEFAULT ) { c = 5; }
    // stuff
}

(但您当然不能将-1作为实际值传递;您也可以使用INT_MAX或其他内容)

选项3:使用重载和枚举(可能会变得混乱):

enum NoVal {
DEFAULT
};
void myFunction( int a = 3, int b = 4, int c = 5 ) {
    // stuff
}
void myFunction( int a = 3, NoVal, int c = 5 ) {
    return myFunction( a, 4, c );
}
void myFunction( NoVal, int b = 4, int c = 5 ) {
    return myFunction( 3, b, c );
}
void myFunction( NoVal, NoVal, int c = 5 ) {
    return myFunction( 3, 4, c );
}

myFunction( 10, DEFAULT, 2 );

选项4:调整功能,使参数更符合逻辑顺序。

答案 4 :(得分:0)

你可以简单地做那样的事情

#define DEF 0
void func(int *a, int *b, int *c) {
  if (a) *a = 5;
  if (b) *b = 2;
  if (c) *c = 3;
}

然后这样称呼:

int x = 9;
int y = 10;
func(&x, DEF, &y);