Why the "in-place constructors" for std::optional are explicit? - constructor

The C++17 standard says that the std::in_place constructors of std::optional must be explicit (the same was with boost::optional). But why?? What is the reason behind this?
It forbids to have a nice code like this:
struct Value
{
Value(int) { }
};
std::optional<Value> giveValueFive()
{
return {std::in_place, 5};
}

Related

ES6 classes inheritance resolving conflicting method names

I want this.method2() call of base to execute base.method2() while it actually executes derived.method2() (I understand the idea behind the behavior), is it possible to achieve this without methods renaming and what is the best practice here?
const base = class {
method() {
this.method2();
}
method2() {
console.error("base.method2");
}
}
const derived = new class extends base {
method() {
super.method();
}
method2() {
console.error("derived.method2");
}
}
derived.method(); // want 'base.method2' here
You can do this with call.
method() {
base.prototype.method2.call(this);
}
In this case you don't technically need to use call and supply the this value, because you don't use it, but it is the best way to create the call you want so it works in all circumstances.
Incidentally, I don't understand what you are seeking to achieve with the const base = class {} statement. A class declaration would be much less surprising: class base {}.

use page as argument in Umbraco

I'm trying to pass page as argument in Umbraco . and in a helper I need some properties of the page . like Name , ...
This is my code :
var PageWeAreInheritedFrom = CurrentPage;
#ShowBanner(PageWeAreInheritedFrom);
#helper ShowBanner(dynamic pageWeRIn)
{
if (pageWeRIn.bannerIsInherited)
{
#ShowBanner(pageWeRIn.Parent)
}
else
{
//here I want to have a switch case based on pageWeRIn.Name
//but I cant have it.
}
}
This is the Error .seems the page type is different in the helper method
A switch expression or case label must be a bool, char, string,
integral, enum, or corresponding nullable type
This is caused because pageWeRIn is dynamic and C#'s switch can't work with dynamic variables. I personally don't work with dynamics in my views but only with typed models. For more information see: http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/
A typed implementation would look somehthing like this (not tested):
#ShowBanner(Mode.Content);
#helper ShowBanner(IPublishedContent pageWeRIn)
{
if (pageWeRIn.GetPropertyValue<bool>("bannerIsInherited"))
{
#ShowBanner(pageWeRIn.Parent)
}
else
{
//use all the switches you want on pageWeRIn.Name
}
}
Another way to do it without changing the whole code would be to introduce a new variable that's typed (as Jannik explained in his comment) and then use a switch
string nodeName = pageWeRIn.Name
switch(nodeName){
// whatever
}

ES6: If class has method?

I have a class with a method that calls other methods:
class MyClass {
build(methods) {
methods.forEach((method) => {
if (this.method) {
this.method();
}
});
}
stuff() {}
moreStuff() {}
}
const a = MyClass();
a.build(['stuff', 'moreStuff']);
I haven't been able to find any reference on any special methods for classes. My first thought was to use hasOwnProperty (however eslint nags me that I shouldn't use it within the class). The approach above wouldn't work reliably as classes have built-in functions.
I was also looking at Reflect as possibly being my saving grace, but I could really use some guidance on what is the best practice for this scenario?
I think you're looking for
build (methodnames) {
for (const methodname of methodnames) {
if (typeof this[methodname] == "function") {
this[methodname]();
}
}
}
There's nothing special about classes - and in fact you should ignore them. If you want to call some method, the only thing that is important is that there is a function as a property value. It doesn't matter whether the method is an own property of the prototype object of the class that created the instance.

ActionScript this inside nested function

ActionScript 3 language specification states:
In ECMA-262 edition 3, when this appears in a nested function, it is bound to the global object if the function is called lexically, without an explicit receiver object. In ActionScript 3.0, this is bound to the innermost nested this when the function is called lexically.
(Source: http://help.adobe.com/livedocs/specs/actionscript/3/wwhelp/wwhimpl/js/html/wwhelp.htm)
However I tried the following and my result is not what I expected from the sentence above - the this inside the nested function is bound to the global object:
function f():void
{
trace("f() this.a", this.a); // "ok"
function g():void { trace("g() this.a", this.a); } // "undefined"
g();
}
f.call( { a: "ok" } );
Either the documentation is wrong here or I didn't understand it correctly. Can you explain me?
I believe the case is the reference to the Object, not the nested function concept.
If you try:
function f(target:Object):void
{
trace("f() this.a", target.a); // "ok"
function g():void { trace("g() this.a", target.a); } // "ok"
g();
}
f( { a: "ok" } );

ANTLR v3 C# namespaces

Hopefully this is a really quick one ;) I have written a lexer / parser specification in ANTLR3, and am targeting the CSharp2 target. The generated code works correctly, but I can't get ANTLR to put the C# output into a namespace.
The relevant section of the Grammar file is as follows:
grammar MyGrammar;
options
{
language = CSharp2;
output = AST;
ASTLabelType = CommonTree;
}
To generate the correct namespace, I have tried:
#namespace { MyNamespace }
and
#lexer::namespace { MyNamespace }
#parser::namespace { MyNamespace }
but both of these generate errors, claiming that the file has no rules.
Any help is appreciated.
I use this for a combined lexer and parser (and it generates the namespace correctly):
grammar Test;
options
{
language=CSharp2;
}
#lexer::namespace {
My.Name.Space
}
#parser::namespace {
My.Name.Space
}
DIGIT : '0'..'9';
simple : DIGIT EOF;
So i wonder why your version didn't work - maybe you want to try this simple example and see if it works for you.
It seems that the #namespace directive needs to be placed AFTER the tokens{} block. All good now...
With language = 'CSharp3'; (and CSharp2 as well), you can do:
#lexer::namespace {
My.Name.Space
}
#parser::namespace {
My.Name.Space
}
which generates:
} // namespace
My.Name.Space <-- compile error here
at the end of lexer and parser code. If I write:
#lexer::namespace {My.Name.Space}
#parser::namespace {My.Name.Space}
it works fine and generates:
} // namespace My.Name.Space <-- within the line comment, no error of course