default-arguments

default arguments to template's expressions

隐身守侯 提交于 2019-12-11 14:58:37
问题 If I want to make a template that can accept 2 untyped arguments, and pass them through do notation, when I omit the second do I'd like to have a way to specify a fallback in the form of a parameter's default value. as such: template tpl(x: bool, body: untyped, bodyFinally: untyped): void = if x: body else: bodyFinally #call site: var r: int tpl(true) do: r = 2 do: raise newException(Exception, "") This works, but that: template tpl(x: bool, body: untyped, bodyFinally: untyped = discard):

Overloaded parent class constructors. Wrong inizializer choice?

别来无恙 提交于 2019-12-11 14:56:46
问题 I would like to add a Child class to a pre-existing project having Parent already defined and declared. Parent class has got two constructors with initializer-list. This is my code, and it generates error C2668: 'Parent::Parent' : ambiguous call to overloaded function. Where is my error? Thanks @Mape for your snippet #include <stdio.h> class Parent { public: // Here a constructor with one default trailing argument, i.e. myInt, // myDouble is not initialised. This is correct, as once one

Get function's default value?

假如想象 提交于 2019-12-09 14:20:03
问题 Is there a way to retrieve a function's default argument value in JavaScript? function foo(x = 5) { // things I do not control } Is there a way to get the default value of x here? Optimally, something like: getDefaultValues(foo); // {"x": 5} Note that toString ing the function would not work as it would break on defaults that are not constant. 回答1: Since we don't have classic reflection in JS, as you can find on C#, Ruby, etc., we have to rely on one of my favorite tools, regular expressions,

Have I misunderstood the scope of this default argument shared_ptr?

北慕城南 提交于 2019-12-08 19:25:25
问题 Take a look at this: #include <iostream> #include <memory> using Foo = int; using FooPtr = std::shared_ptr<Foo>; FooPtr makeFoo() { FooPtr f{ new Foo(), [](Foo* ptr) { delete ptr; std::cerr << "!\n"; } }; return f; } void bar(FooPtr p = {}) { p = makeFoo(); } int main() { bar(); } // Expected output: '!' // Failure case: no output (deleter not invoked?) I expected the shared_ptr deleter to be called when bar() returns, and on my 64-bit CentOS 7 system using GCC 4.8.5, it does. However, on my

Lambda function as a default argument for std::function in constructor

你离开我真会死。 提交于 2019-12-07 04:07:04
问题 I'd like to have a default functor for a functor parameter in the constructor of a class. As a minimal example I came up with a class which should server as a filter, which filters elements of type T iif a filter function returns true. The filter function should be provided in the constructor, defaulting to an "accept all" filter function: template<class T> class Filter { public: typedef std::function<bool(const T&)> FilterFunc; Filter(const FilterFunc & f = [](const T&){ return true; }) : f

Verifying mocked object method calls with default arguments

不想你离开。 提交于 2019-12-07 01:40:49
问题 Suppose I have this class: class Defaults { def doSomething(regular: String, default: Option[String] = None) = { println(s"Doing something: $regular, $default") } } I want to check that some other class invokes doSomething() method on Defaults instance without passing second argument: defaults.doSomething("abcd") // second argument is None implicitly However, mocking Defaults class does not work correctly. Because default values for method arguments are compiled as hidden methods in the same

Template struct with the default template argument is not instantiated

纵饮孤独 提交于 2019-12-05 18:43:02
Let's say I have this code template<typename T2, typename T = int> struct X { static double f; }; template<typename T> double X<T>::f = 14.0; If I try to compile clang give me the following error nested name specifier 'X::' for declaration does not refer into a class, class template or class template partial specialization and for GCC : error: template definition of non-template 'double X::f' The question is : Why the compiler want us to specialize the struct X like that : template<typename T2> struct X<T2,int> { static double f; }; The first declaration has int as a default argument, why the

Lambda function as a default argument for std::function in constructor

余生长醉 提交于 2019-12-05 10:28:20
I'd like to have a default functor for a functor parameter in the constructor of a class. As a minimal example I came up with a class which should server as a filter, which filters elements of type T iif a filter function returns true. The filter function should be provided in the constructor, defaulting to an "accept all" filter function: 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; }; I instantiate the template class like the following: int main() {

Template parameter default to a later one

落花浮王杯 提交于 2019-12-05 08:41:53
This link doesn't answer my question so I'll ask it here: Basically I want to write a template function template <typename Out, typename In> Out f(In x); Here I always need to specify Out when calling f . I don't want to do it every time, so I basically want template <typename Out = In, typename In> Out f(In x); Which means if I don't specify Out , it will default to In . However, this is not possible in C++11. So my question is, is there any way to achieve the effect: calling f(t) will instantiate f<T,T>(t) or more generally f<typename SomeThing<T>::type, T> calling f<U>(t) will instantiate f

Am I Allowed to Default a Template Argument in a Forward Declaration

烈酒焚心 提交于 2019-12-05 02:06:21
问题 So I'm trying to understand what's happening with Boost's ptree implementation. In ptree.hpp basic_ptree is actually defined: template<class Key, class Data, class KeyCompare> class basic_ptree In ptree_fwd.hpp there is what looks like a forward declaration of basic_ptree but with a new template argument default: template < class Key, class Data, class KeyCompare = std::less<Key> > class basic_ptree; And finally in ptree_fwd.hpp ptree is typedef 'd: typedef basic_ptree<std::string, std: