这是我的代码:
struct S
{
int f() { return 1; }
int g(int arg = f()) { return arg; }
};
int main()
{
S s;
return s.g();
}
Run Code Online (Sandbox Code Playgroud)
这无法编译错误:
error: cannot call member function 'int S::f()' without object
Run Code Online (Sandbox Code Playgroud)
尝试this->f()也不起作用,因为this可能不会在该上下文中使用.
有没有办法使这个工作仍然使用默认参数?
当然可以通过不使用默认参数来解决它:
int g(int arg) { return arg; }
int g() { return g(f()); }
Run Code Online (Sandbox Code Playgroud)
然而,考虑到在"真实代码"之前有更多参数arg,以及遵循这种模式的几个函数,这会变得冗长.(如果在一个函数中有多个默认参数,那就更难看了).
NB.这个问题起初看起来很相似,但实际上他问的是如何形成一个闭包,这是一个不同的问题(并且链接的解决方案不适用于我的情况).
我试图在Visual C++ 2010中实现智能相等测试宏类型模板函数时遇到了一些麻烦,该函数与VS中关于模板函数的默认参数的错误有关.我通过在额外的函数中包装参数的值来修复它,但现在我发现我不能在一行中使用该函数两次!
头文件:
// example.h
#pragma once
#include <limits>
namespace myspace
{
// Need to define this separately to avoid a Visual Studio bug
template<typename T> T epsilon() { return std::numeric_limits<T>::epsilon(); }
// A generic equality test
template<typename T> inline bool smartEqual(
const T &v1,
const T &v2,
const T &eps = epsilon<T>())
{
return (v1 == v2);
}
// Template specialization for floating-point numbers
template<> bool smartEqual<float>(
const float &v1,
const float &v2,
const float &eps); …Run Code Online (Sandbox Code Playgroud) C++标准第8.3.6.4节说明了这一点
对于非模板函数,可以在稍后的同一范围内的函数声明中添加默认参数.[...]
但我的问题是,为什么它不允许模板功能?不允许在模板函数的同一范围内的后续声明中添加默认参数的基本原理是什么?
考虑这个编译好的程序.(非模板功能)(请参阅此处的现场演示.)
#include <iostream>
int f(int a,int b,int c=3);
int f(int a,int b=9,int c); // default argument in middle, ok allowed
int main()
{
f(3);
f(3,6);
f(3,6,9);
return 0;
}
int f(int a,int b,int c)
{
std::cout<<a<<' '<<b<<' '<<c<<'\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
template <typename T>
void f(T a,int b,int c=3);
template <typename T>
void f(T a,int b=9,int c); // compiler error why???
int main()
{
f(3);
f(3,6); …Run Code Online (Sandbox Code Playgroud) c++ templates language-lawyer function-templates default-arguments
有没有办法在JavaScript中检索函数的默认参数值?
function foo(x = 5) {
// things I do not control
}
Run Code Online (Sandbox Code Playgroud)
有没有办法获得x这里的默认值?最理想的是,像:
getDefaultValues(foo); // {"x": 5}
Run Code Online (Sandbox Code Playgroud)
请注意,toString该函数不起作用,因为它会在非常量的默认值上中断.
可以在C++中添加或重新定义函数的默认参数.我们来看看这个例子:
void foo(int a, int b, int c = -1) {
std::cout << "foo(" << a << ", " << b << ", " << c << ")\n";
}
int main() {
foo(1, 2); // output: foo(1, 2, -1)
// void foo(int a, int b = 0, int c);
// error: does not use default from surrounding scope
void foo(int a, int b, int c = 30);
foo(1, 2); // output: foo(1, 2, 30)
// void foo(int a, int b, …Run Code Online (Sandbox Code Playgroud) 我有一个模板功能,我们称之为"客户端":
template<typename T>
void client(T (*func)(const std::string&), const std::string& s) {}
Run Code Online (Sandbox Code Playgroud)
然后有许多"adaptee"函数都具有相同类型的第一个非默认参数,但以下参数的数量不同并具有默认值:
void adaptee_one(const std::string&, int i = 1, char* c = nullptr) {}
void adaptee_two(const std::string&, float* f = nullptr) {}
Run Code Online (Sandbox Code Playgroud)
以上功能是给定的.现在我想做的是将它们client<>()作为第一个参数传递给上面的函数,我只关心传递第一个参数,const std::string&.所以我做了以下事情:
void bindAdapteeOne(const std::string& s) {
return adaptee_one(s);
}
void bindAdapteeTwo(const std::string& s) {
return adaptee_two(s);
}
Run Code Online (Sandbox Code Playgroud)
然后bindAdapteeX()转到client<>().
我想做的是自动化包装或者有一个(模板化的)包装而不是每个适配器.我觉得可变性可能就是这种情况,但对如何准确应用它们几乎一无所知.
C++ 11很好,如果绝对必要,C++ 14就可以了.
在PHP中,您可以调用函数.通过使用这些语句不调用所有参数.
function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
echo "$t1, $t2, $t3";
}
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的功能
test();
Run Code Online (Sandbox Code Playgroud)
所以我只想说我希望最后一个是不同的而不是其他的.我能做到的唯一方法是做到这一点,但没有成功:
test('test1','test2','hi i am different');
Run Code Online (Sandbox Code Playgroud)
我试过这个:
test(,,'hi i am different');
test(default,default,'hi i am different');
Run Code Online (Sandbox Code Playgroud)
做这样的事情的最佳方法是什么?
Scala-lang参考5.5.1和6.6.1给我的印象是默认参数能够引用先前评估的参数:
class Test(val first: String, val second: String = first)
Run Code Online (Sandbox Code Playgroud)
但从实验来看,似乎唯一的方法就是使用表格:
class Test(val first: String)(val second: String = first)
Run Code Online (Sandbox Code Playgroud)
然后定义辅助构造函数或创建伴随类,以避免在创建时指定第二组括号.我真的不明白第二个构造函数是如何工作的,它看起来像一个curried函数所以我可能会猜测有必要first独立评估second,这是正确的吗?这个形式是必要的还是有一些合成糖可以用来调整第一个构造函数来做我想要的?
constructor scala operator-precedence default-parameters default-arguments
根据C++标准,当使用默认模板参数向前声明模板类型时,它们中的每一个都只能出现在一个声明中.例如:
// GOOD example
template <class T = void>
class Example; // forward-declaration
template <class T>
class Example {}; // definition
Run Code Online (Sandbox Code Playgroud)
// GOOD example
template <class T>
class Example; // forward-declaration
template <class T = void>
class Example {}; // definition
Run Code Online (Sandbox Code Playgroud)
// BAD example
template <class T = void>
class Example; // forward-declaration
template <class T = void> // ERROR: template parameter redefines default argument
class Example {}; // definition
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我在不同的文件中有很多前向声明,所以将默认参数放在定义中是有意义的:
// foo.hpp, bar.hpp, baz.hpp, etc. …Run Code Online (Sandbox Code Playgroud) c++ ×7
templates ×3
constructor ×2
c++11 ×1
c++14 ×1
clang ×1
ecmascript-6 ×1
javascript ×1
php ×1
scala ×1