我们希望使我们的C++大括号样式更加一致.现在,我们的代码包含以下内容:
if (cond)
{
// ...
}
else
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
...和:
if (cond) {
// ...
} else {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我们想要专门使用后一种风格.
但是,我们不希望改变我们的代码的缩进.我尝试过使用astyle,bcpp,GNU indent和Uncrustify(我看过GreatCode的命令行选项).不幸的是,这些工具中的每一个都坚持重新定义我们的代码,并且大多数工具都非常糟糕地破坏了C++构造函数初始化程序列表和预处理程序宏.
是否有任何C++代码美化器可以修复大括号而只留下缩进?它不一定是一个预先存在的工具 - 如果你知道如何使用一些疯狂的Perl单线程,这也很好.谢谢!
更新:是的,我们知道这将使得难以读取旧代码的差异.这是一个长期推迟的代码清理,我们已经确定一致格式化的日常优势超过任何版本控制困难.
回到学校,我们写了一个编译器,其中花括号具有执行所有表达式的默认行为,并返回最后一个值...所以你可以这样写:
int foo = { printf("bar"); 1 };
Run Code Online (Sandbox Code Playgroud)
在C#中有相同的东西吗?例如,如果我想编写一个具有副作用的lambda函数.
关于lambda副作用的一点(仅仅是一个例子),如果有这个功能则更多...例如在lisp中,你有预测
I was surprised to see that the following doesn't work as expected.
define('CONST_TEST','Some string');
echo "What is the value of {CONST_TEST} going to be?";
Run Code Online (Sandbox Code Playgroud)
outputs: What is the value of {CONST_TEST} going to be?
Is there a way to resolve constants within curly braces?
Yes, I am aware I could just do
echo "What is the value of ".CONST_TEST." going to be?";
Run Code Online (Sandbox Code Playgroud)
but I'd prefer not to concatanate strings, not so much for performance but for readability.
我刚刚开始使用emacs,并且有一个我非常喜欢的功能,并且搜索一下也没用.我希望其他人这样做是因为我还不想学习elisp.
void foo()<cursor>
Run Code Online (Sandbox Code Playgroud)
我想输入一个"{"来导致这种情况发生
void foo(){
<cursor>
}
Run Code Online (Sandbox Code Playgroud)
我希望这只发生在cc模式中,并且只在不在字符串/ comment/etc中的行尾
我想到的第一件事是重新绑定"{"来做到这一点(我可以弄清楚如何自己做),但很难让它只在恰当的时间发生.
任何提示将不胜感激.
这个问题与这里讨论的问题有关.
我尝试使用初始化列表来创建要传递给的参数operator[]
.
#include <string>
#include <vector>
struct A {
std::string& operator[](std::vector<std::string> vec)
{
return vec.front();
}
};
int main()
{
// ok
std::vector<std::string> vec {"hello", "world", "test"};
A a;
// error: could not convert '{"hello", "world", "test"}' to 'std::vector...'
a[ {"hello", "world", "test"} ];
}
Run Code Online (Sandbox Code Playgroud)
我的编译器(GCC 4.6.1)抱怨:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20:8: error: expected primary-expression before ‘{’ token
test.cpp:20:8: error: expected ‘]’ before ‘{’ token
test.cpp:20:8: error: expected ‘;’ before ‘{’ token …
Run Code Online (Sandbox Code Playgroud) 在Java字节代码级别,简单的if语句(示例1)和普通的if语句(示例2)之间是否有任何区别:
例1:
if (cond) statement;
Run Code Online (Sandbox Code Playgroud)
例2:
if (cond) {
statement;
}
Run Code Online (Sandbox Code Playgroud)
问题的背景是,我在"高性能"类中看到过,java.awt.Rectangle
而且Point
只看到没有花括号的变体.
是否有任何速度优势,或者只是代码风格?
在下面的代码中,我有一个while语句用于确保输入字符串少于10个字符.我已经声明了一个bool
被叫cont
,我用它来告诉while循环在我的条件满足后停止.
#include "stdafx.h"
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "Enter a string less than 10 characters long: ";
string teststring;
{
bool cont(false);
//if input is 10 or more characters, ask for input again until it is less
while (!cont)
{
getline(cin, teststring);
if (teststring.length() >= 10)
{
cout << "Too long, try again: ";
}
else
{
cout << "Thank you.\n\n";
cont = true;
}
}
}
return 0; …
Run Code Online (Sandbox Code Playgroud) 我是一名计算机科学专业的学生,不久前我们的教授向我们解释说,在C语言中,当只有一个语句时我们可以删除大括号:
if (a)
do b
Run Code Online (Sandbox Code Playgroud)
但我们做不到这样的事情:
if(a)
do b
do c
Run Code Online (Sandbox Code Playgroud)
因为那会做不止一个陈述.
但它也告诉我们,删除花括号有一个例外,即使它只是一个语句,我们也做不到.我做了很多搜索,但我发现的唯一一件事就是我不能在do-while循环中做到这一点,但我们在讨论if语句,有什么帮助吗?
编辑:我们也在谈论嵌套的if语句,也许是关于那个?
curly-braces ×10
c++ ×3
c ×2
if-statement ×2
syntax ×2
bytecode ×1
c# ×1
c++11 ×1
cc-mode ×1
coding-style ×1
const ×1
emacs ×1
formatting ×1
indentation ×1
java ×1
javascript ×1
jsdoc ×1
loops ×1
nested-if ×1
notation ×1
performance ×1
php ×1
scope ×1
while-loop ×1