literals

How to set bool pointer to true in struct literal?

╄→尐↘猪︶ㄣ 提交于 2021-02-06 09:39:10
问题 I have the function below which accepts a bool pointer. I'm wondering if there is any notation which allows me to set the value of the is field to true in the struct literal; basically without to define a new identifier (i.e. var x := true ; handler{is: &x} ) package main import "fmt" func main() { fmt.Println("Hello, playground") check(handler{is: new(bool) }) } type handler struct{ is *bool } func check(is handler){} 回答1: You can do that but it's not optimal: h := handler{is: &[]bool{true}

-9'223'372'036'854'775'808LL is unsigned

时间秒杀一切 提交于 2021-02-04 05:56:50
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

-9'223'372'036'854'775'808LL is unsigned

╄→尐↘猪︶ㄣ 提交于 2021-02-04 05:56:19
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

Convert a C# string value to an escaped string literal with Roslyn

左心房为你撑大大i 提交于 2021-01-28 06:11:33
问题 There is such method for CodeDom by @Hallgrim found here: private static string ToLiteral(string input) { using (var writer = new StringWriter()) { using (var provider = CodeDomProvider.CreateProvider("CSharp")) { provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null); return writer.ToString(); } } } Nowadays we need a Roslyn remake for .NET Core. Or should we manually replace symbols? 回答1: You can create a SyntaxNode ( LiteralExpressionSyntax ) from the input

Typescript: Enforce a type to be “string literal” and not <string>

こ雲淡風輕ζ 提交于 2021-01-21 08:21:00
问题 Problem Is there a way in Typescript to define a type that is only a string literal, excluding string itself? Note that I am not talking about a certain list of string literal; for which, a simple union of "Value1" | "Value2" , or an enum type would work. I am talking about any string literal, but not string itself. Example Code type OnlyStringLiterals = ...; // <--- what should we put here? const v1: OnlyStringLiterals = "hi"; // should work const v2: OnlyStringLiterals = "bye"; // should

Typescript: Enforce a type to be “string literal” and not <string>

主宰稳场 提交于 2021-01-21 08:19:52
问题 Problem Is there a way in Typescript to define a type that is only a string literal, excluding string itself? Note that I am not talking about a certain list of string literal; for which, a simple union of "Value1" | "Value2" , or an enum type would work. I am talking about any string literal, but not string itself. Example Code type OnlyStringLiterals = ...; // <--- what should we put here? const v1: OnlyStringLiterals = "hi"; // should work const v2: OnlyStringLiterals = "bye"; // should

Why does auto deduce this variable as double and not float? [duplicate]

时光怂恿深爱的人放手 提交于 2021-01-19 14:24:14
问题 This question already has answers here : Why floating point value such as 3.14 are considered as double by default in MSVC? (5 answers) All floats are doubles? (3 answers) How a floating point literal is treated either double or float in Visual C++? (2 answers) why sizeof(13.33) is 8 bytes? (5 answers) What is the type of the value 1.0e+1 (4 answers) Closed 1 year ago . In the snippet below, auto deduces the variable to double , but I want float . auto one = 3.5; Does it always use double for

Why does auto deduce this variable as double and not float? [duplicate]

两盒软妹~` 提交于 2021-01-19 14:23:34
问题 This question already has answers here : Why floating point value such as 3.14 are considered as double by default in MSVC? (5 answers) All floats are doubles? (3 answers) How a floating point literal is treated either double or float in Visual C++? (2 answers) why sizeof(13.33) is 8 bytes? (5 answers) What is the type of the value 1.0e+1 (4 answers) Closed 1 year ago . In the snippet below, auto deduces the variable to double , but I want float . auto one = 3.5; Does it always use double for