curly-braces

std::array<T> initialization

荒凉一梦 提交于 2019-11-29 06:11:23
A std::array<T> is essentially a C-style array wrapped in a struct . The initialization of struct s requires braces, and the initialization of arrays requires braces as well. So I need two pairs of braces: std::array<int, 5> a = {{1, 2, 3, 4, 5}}; But most of the example code I have seen only uses one pair of braces: std::array<int, 5> b = {1, 2, 3, 4, 5}; How come this is allowed, and does it have any benefits or drawbacks compared to the first approch? The benefit is that you have ... less to type. But the drawback is that you are only allowed to leave off braces when the declaration has

Extra brace brackets in C++ code

给你一囗甜甜゛ 提交于 2019-11-29 03:43:32
Sometimes you run into code that has extra brace brackets, that have nothing to do with scope, only are for readability and avoiding mistakes. For example: GetMutexLock( handle ) ; { // brace brackets "scope" the lock, // must close block / remember // to release the handle. // similar to C#'s lock construct } ReleaseMutexLock( handle ) ; Other places I have seen it are: glBegin( GL_TRIANGLES ) ; { glVertex3d( .. ) ; glVertex3d( .. ) ; glVertex3d( .. ) ; } // must remember to glEnd! glEnd() ; This introduces a compiler error if the mutex isn't freed (assuming you remember both the } and the

C# Switch statement with/without curly brackets… what's the difference?

眉间皱痕 提交于 2019-11-28 18:31:06
Has C# always permitted you to omit curly brackets inside a switch() statement between the case: statements? What is the effect of omitting them, as javascript programmers often do? Example: switch(x) { case OneWay: { // <---- Omit this entire line int y = 123; FindYou(ref y); break; } // <---- Omit this entire line case TheOther: { // <---- Omit this entire line double y = 456.7; // legal! GetchaGetcha(ref y); break; } // <---- Omit this entire line } Dirk Vollmar Curly braces are not required, but they might come in handy to introduce a new declaration space . This behavior hasn't changed

Format Curly Braces on Same Line in C++ VSCode

眉间皱痕 提交于 2019-11-28 16:16:53
问题 I'm using the C++ Extension for VSCode (Visual Studio Code) . Currently, I have the setting "C_Cpp.clang_format_formatOnSave" set to true . This format's my code when I save my C++ file. But the format results in curly braces on new lines rather than on the same line. Current C++ VSCode Formatted for (int i = 0; i < 10; i++) { // ... } What I Want C++ VSCode Formatted Code to Look Like for (int i = 0; i < 10; i++) { // ... } I also have editor.wrappingIndent set to "same" . How can I make

How do curly braces and scope wоrk in C?

纵然是瞬间 提交于 2019-11-28 12:05:06
问题 I'm currently trying to learn some C in my spare time. I have some experience in Java, and I'm therefore used to limit scoping of i.e. variables with curly braces. But I'm a bit confused, as it seems like Brian W. Kernighan/Dennis M. Ritchie's book "The C Programming Language" doesn't use a lot of curly braces, where I would assume it's normal to use them (from a Java perspective). E.g. 1.6 in the book where the code is: while((c = getchar())) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']

When do you use code blocks?

折月煮酒 提交于 2019-11-28 08:35:39
问题 When do you use code blocks in C/C++/C#, etc.? I know the theoretical reason behind them, but when do you use them in real programs? EDIT : I have just realised that I use them in switch statements, where variables would otherwise be in the same scope (grr for things like i ): switch (x) { case "abc": { /* code */ } break; } etc (Just to clarify, in a switch statement, the extra braces are NOT required.) Related: Do you use curly braces for additional scoping? (https://stackoverflow.com

JavaScript formatting: must braces be on the same line as the if/function/etc keyword? [duplicate]

梦想的初衷 提交于 2019-11-28 08:30:15
问题 Possible Duplicate: why results varies upon placement of curly braces in javascript code We have company policies that dictate that in PHP opening curly braces should be on their own lines for readability and so that they can line-up with the closing brace; thus: if (true) { ... } but in JS they should be kept on the same line, in case there are problems with browsers incorrectly interpretting it . if (true) { ... Is the above italic part a legitimate concern? PS - I suspect that this

Escape property reference in Spring property file

青春壹個敷衍的年華 提交于 2019-11-28 07:22:06
问题 I want to escape my Spring propeties file in order to get in my bean property: ${ROOTPATH}/relativePath I have a simple Spring config file that contains: <context:property-placeholder location="classpath:myprops.properties" /> <bean id="myBean" class="spring.MyBean"> <property name="myProperty" value="${myproperty}" /> </bean> The myprops.properties contains: myproperty=\${ROOTPATH}/relativePath The above setup returns: Could not resolve placeholder 'ROOTPATH' . I tried a lot of possible

What is the meaning of curly braces? [closed]

懵懂的女人 提交于 2019-11-28 06:13:16
Just starting to figure Python out. I've read this question and its responses: Is it true that I can't use curly braces in Python? and I still can't fathom how curly braces work, especially since pages like Simple Programs: http://wiki.python.org/moin/SimplePrograms use curly braces all over the place. I understand square brackets and regular curved parentheses, but I don't know what's meant by "defining dictionaries" or what they're supposed to represent. Brenton Alker "Curly Braces" are used in Python to define a dictionary. A dictionary is a data structure that maps one value to another -

How to make eclipse automatically add braces to an IF statement?

匆匆过客 提交于 2019-11-28 06:10:31
In Java the following is completely valid: if (x == null) Y(); else Z(); I personally don't like it at all. I like all my IF statements to have braces: if (x == null) { Y(); } else { Z(); } The eclipse formatter is wonderful and can beautify my code in many other ways. Is there a way to have it add the braces to IF statements? Under "Preferences": Java > Editor > Save Actions 1) Check "Additional actions" 2) Click "Configure…" 3) Go to the "Code Style" tab 4) Check "Use blocks in if/while/for/do statements" and configure to your preferences Yes. Eclipse menu: Source -> Clean Up... Configure...