通常在ANSI C代码中,我可以看到括号中的单个返回值.
像这样:-
int foo(int x) {
if (x)
return (-1);
else
return (0);
}
Run Code Online (Sandbox Code Playgroud)
为什么在这些情况下使用返回值周围的?有任何想法吗?我认为没有理由.
我有两个变量,我想分别处理较大和较小的一个。
我的方法:
a = 1;
b = 2;
if (a >= b){
int c = a;
int d = b;
}
else{
int c = b;
int d = a;
}
Run Code Online (Sandbox Code Playgroud)
我获得的未使用的变量的错误,当我尝试使用c和d以后,它说
c无法解决
有办法解决吗?
我经常看到这样的代码(C,C++,有时候是Java):
return (value);
Run Code Online (Sandbox Code Playgroud)
我认为这些括号没有任何好处.所以我的问题是,假设程序员return是某种以返回值作为参数的函数,或者这些括号是否有意义?
我知道这里已经提出了类似的问题,但这只与ANSI C有关.我想知道是否有特定于C++或Java的方面尚未得到解答.
情况1:
#include <iostream>
decltype(auto) fun()
{
std::string str = "In fun";
return str;
}
int main()
{
std::cout << fun() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这里,程序在Gcc编译器中工作正常.decltype(auto)被推断为类型str.
案例2:
#include <iostream>
decltype(auto) fun()
{
std::string str = "In fun";
return (str); // Why not working??
}
int main()
{
std::cout << fun() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这里,生成以下错误和分段错误:
In function 'decltype(auto) fun()':
prog.cc:5:21: warning: reference to local variable 'str' returned [-Wreturn-local-addr]
std::string str = "In fun";
^~~
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
为什么 …
我正在努力将稀疏矩阵支持添加到开源数学库中,并且不希望对矩阵类型Dense和Sparse矩阵类型都有重复的函数。
下面的示例显示了一个add函数。一个有两个函数的工作示例,然后两次尝试都失败了。下面提供了代码示例的 Godbolt 链接。
我看过了本征文档的编写采取征类型的功能,但其使用的答案Eigen::EigenBase不工作,因为两者MatrixBase并SparseMatrixBase没有提供有特别的方法存在EigenBase
https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
我们使用 C++14,非常感谢您的帮助和时间!!
#include <Eigen/Core>
#include <Eigen/Sparse>
#include <iostream>
// Sparse matrix helper
using triplet_d = Eigen::Triplet<double>;
using sparse_mat_d = Eigen::SparseMatrix<double>;
std::vector<triplet_d> tripletList;
// Returns plain object
template <typename Derived>
using eigen_return_t = typename Derived::PlainObject;
// Below two are the generics that work
template <class Derived>
eigen_return_t<Derived> add(const Eigen::MatrixBase<Derived>& A) {
return A + A;
}
template <class Derived>
eigen_return_t<Derived> add(const Eigen::SparseMatrixBase<Derived>& …Run Code Online (Sandbox Code Playgroud) 我正在开发的iPhone应用程序中有一个奇怪的问题.我希望我的应用程序仅支持纵向模式,但由于某种原因我无法做到(设备和模拟器).
为了仅支持肖像模式我做了如下:
shouldAutorotateToInterfaceOrientation 但正如我所说它不起作用,奇怪的结果是该应用程序支持所有方向(纵向,颠倒,左侧景观,右侧景观).
有任何想法吗?
这是我如何实现的 shouldAutorotateToInterfaceOrientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
NSLog(@"Checking orientation %d", interfaceOrientation);
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)
我刚刚注意到,当我旋转手机时,我收到以下消息:
"不推荐使用两阶段旋转动画.此应用程序应使用更平滑的单级动画."
这是什么意思?
我正在玩弄以下代码,并使用我的 Visual Studio 2017 应用程序和两个不同的在线编译器得到了不同的结果。在发布模式中,visual studio 在这两种情况下都省略了复制/移动,而两个在线编译器只是在没有括号的 return 语句的情况下才这样做。我的问题是:谁是对的,更重要的是基本规则是什么。(我知道您可以将括号与decltype(auto)语法结合使用。但这不是当前的用例)。
示例代码:
#include <iostream>
#include <cstdio>
struct Foo
{
Foo() { std::cout << "default constructor" << std::endl; }
Foo(const Foo& rhs) { std::cout << "copy constructor" << std::endl; }
Foo(Foo&& rhs) { std::cout << "move constructor" << std::endl; }
Foo& operator=(const Foo& rhs) { std::cout << "copy assignment" << std::endl; return *this; }
Foo& operator=(Foo&& rhs) { std::cout << "move assignment" << std::endl; return *this; }
};
Foo foo_normal()
{ …Run Code Online (Sandbox Code Playgroud) 我正在查看有关C ++ 14语言扩展的isocpp.org常见问题解答,内容decltype(auto)如下:
...
注意:
decltype(auto)如上所示,主要用于推导转发函数和类似包装的返回类型,在此情况下,您希望该类型精确“跟踪”正在调用的某些表达式。但是,除此以外,decltype(auto)并不打算被广泛使用。特别是,尽管它可以用来声明局部变量,但是这样做可能只是一个反模式,因为局部变量的引用性不应该依赖于初始化表达式。另外,它对如何编写return语句也很敏感。这两个函数具有不同的返回类型:Run Code Online (Sandbox Code Playgroud)decltype(auto) look_up_a_string_1() { auto str = lookup1(); return str; } decltype(auto) look_up_a_string_2() { auto str = lookup1(); return(str); }第一个返回字符串,第二个返回字符串,
string &它是对局部变量的引用str。
我的问题:示例中的返回类型是否应该相反,我的意思是,括号应该形成一个表达式,其类型应该是非引用(或右值引用)?并且没有括号,说的str意思是“左值引用str”。我错了吗?
我有一些 C++ 代码,我可以在其中切换枚举中的值。我试图用 -Wall -Wextra -Werror 编译它。这在使用 clang 时很好。但是,GCC 抱怨未涵盖默认代码路径。简化版本如下所示:
enum class Types: int {
A,
B,
C
};
bool some_logic(Types val) {
switch (val) {
case Types::A:
return (false);
case Types::B:
return (true);
case Types::C:
return (false);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过在函数末尾添加一个 default case 或另一个 return 语句来处理这个问题。但是,我的问题是为什么 GCC 没有检测到枚举的所有情况都已涵盖?或者换一种说法,海湾合作委员会在这里抱怨是否有正当理由?
我在此处对编译器输出进行了比较。
c++ ×7
c ×2
c++14 ×2
decltype ×2
auto ×1
autorotate ×1
clang++ ×1
coding-style ×1
copy-elision ×1
eigen ×1
gcc ×1
gcc-warning ×1
iphone ×1
java ×1
nrvo ×1
objective-c ×1
syntax ×1
templates ×1
xcode ×1