我有这个代码:
struct A{};
template<class T = A>
struct B {
void foo() {}
};
B b; //Error: missing template arguments before 'b'
//Error: expected ';' before 'b'
//More errors
b.foo()
Run Code Online (Sandbox Code Playgroud)
如果我foo()使用相同的模板'signature'作为模板函数,编译器不会抱怨没有指定模板参数:
struct A {};
struct B {
template<class T = A>
void foo() {}
};
B b; //OK
b.foo()
Run Code Online (Sandbox Code Playgroud)
那么为什么我需要为带有默认参数的模板类指定参数,而不是为模板函数指定?我遗失了一些微妙之处吗?
原因是因为模板参数推断失败肯定.但我想知道原因.
c++ templates default-parameters template-argument-deduction
我有一个抽象类,其参数的默认值.我不想在所有可能的实现的构造函数中重用默认值.
abstract class Place(val place: String = "World")
class Message(val message: String = "Hello", p: String) extends Place(p) {
override def toString = s"$message $place"
}
Run Code Online (Sandbox Code Playgroud)
我想得到什么
new Message("Hi", "Universe") = "Hi Universe" // Ok
new Message("Hi") = "Hi World" // Doesn't work, second parameter is required
new Message() = "Hello World" // Doesn't work, second parameter is required
Run Code Online (Sandbox Code Playgroud)
我考虑使用省略第二个参数的辅助构造函数,但它没有帮助,因为你不能在主构造函数之外调用超级构造函数.
我想知道怎么做,或者为什么不可能.我不是在寻找一种解决方法,比如不使用继承.
默认模板参数是否可以以不从右开始的方式使用"默认值"?
标准是什么?
编译器将如何解释?
例如,我很惊讶这段代码有效.
#include <iostream>
using namespace std;
template <bool T=true, class U> //"default" from LEFT-most parameter
void f(U u){
if(T){ cout<<true;}
else cout<<false;
}
int main() {
auto x = []( ){ };
f(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里查看现场演示:https://ideone.com/l6d9du
我有类型的功能
virtual void foo(bla, bla, bla, std::shared_ptr<LoggerInterface> logger) = 0;
Run Code Online (Sandbox Code Playgroud)
我想用NULL指针传递一个默认参数,如:
virtual void foo(bla, bla, bla, std::shared_ptr<LoggerInterface> logger = NULL) = 0;
Run Code Online (Sandbox Code Playgroud)
所以在实现中,如果logger是NULL我什么都不做,否则我使用记录器.
我试图找一个解决方案,但找不到..
UPD:重复声明是无关紧要的,我问的是默认的NULL参数.
gcc 4.4有可能不支持nullptr吗?
通过一个类似的问题感到惊讶(和共同),我自己尝试了标准中提到的问题的例子:
template <typename T, typename U = int> struct S;
template <typename T = int, typename U> struct S
{ void f() { std::cout << __PRETTY_FUNCTION__ << '\n'; } };
int main()
{
S s; s.f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印 void S<int, int>::f() [T = int, U = int] 使用gcc HEAD 8.0.1 201803编译,但无法使用clang HEAD 7.0.0进行编译,除非在实例化期间使用尖括号:
S s; s.f(); // error: declaration of variable 's' with deduced type 'S' requires an initializer
S<> t; t.f(); // …Run Code Online (Sandbox Code Playgroud) 我需要将可为空参数传递给仅接受不可为空对象但定义了默认值的函数。
目前我正在使用 let:
fun myFun(a:String = "qqq"): Whatever {...}
val myString:String? = getNullableString()
val myFunResult = myString.?let{myFun(it)}?:myFun()
Run Code Online (Sandbox Code Playgroud)
这是很冗长的,并且当有多个可选参数时就不可能了。我需要类似的东西
val myFunResult = myFun(myString?:default)
Run Code Online (Sandbox Code Playgroud)
有没有一种实用的方法可以做到这一点?
假设我有一堂课
class C {
C(int a=10);
};
Run Code Online (Sandbox Code Playgroud)
为什么我打电话
C c;
Run Code Online (Sandbox Code Playgroud)
C(int =10)调用构造函数,如果我调用
C c();
Run Code Online (Sandbox Code Playgroud)
调用默认构造函数?怎么避免这个?我想只执行我的构造函数,我试图将默认构造函数设为私有,但它不起作用.
有人可以解释为什么下面的代码的结果将是"B类:: 1"?
为什么派生类的虚方法使用基类的默认参数而不是自己的默认参数?对我来说这很奇怪.提前致谢!
码:
#include <iostream>
using namespace std;
class A
{
public:
virtual void func(int a = 1)
{
cout << "class A::" << a;
}
};
class B : public A
{
public:
virtual void func(int a = 2)
{
cout << "class B::" << a;
}
};
int main()
{
A * a = new B;
a->func();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 以下代码mypy按预期被拒绝:
def foo(value: int) -> None:
print(value, type(value))
foo(None)
Run Code Online (Sandbox Code Playgroud)
输出:
error: Argument 1 to "foo" has incompatible type "None"; expected "int"
Run Code Online (Sandbox Code Playgroud)
但是在引入了一个默认参数之后None,就没有错误了:
def foo(value: int=None) -> None:
print(value, type(value))
foo(None)
Run Code Online (Sandbox Code Playgroud)
如果我们从to更改,我希望mypy只允许None(作为参数和默认值),但似乎不需要这样做。为什么?valueintOptional[int]
这是一个带有默认参数的函数声明:
void func(int a = 1,int b = 1,...,int x = 1)
Run Code Online (Sandbox Code Playgroud)
func(1,1,...,2)
当我只想设置x参数时,如何避免调用,以及使用之前的默认参数设置其余参数?
例如,就像 func(paramx = 2, others = default)
c++ ×7
templates ×3
c++11 ×2
c++17 ×1
constructor ×1
derived ×1
inheritance ×1
kotlin ×1
mypy ×1
nonetype ×1
python ×1
scala ×1
shared-ptr ×1
template-argument-deduction ×1
virtual ×1