相关疑难解决方法(0)

围绕返回值的括号

通常在ANSI C代码中,我可以看到括号中的单个返回值.

像这样:-

int foo(int x) {
  if (x)
    return (-1);
  else
    return (0);
}
Run Code Online (Sandbox Code Playgroud)

为什么在这些情况下使用返回值周围的?有任何想法吗?我认为没有理由.

c syntax coding-style

67
推荐指数
7
解决办法
2万
查看次数

为什么不能将一个变量的值分配给另一个变量?

我有两个变量,我想分别处理较大和较小的一个。

我的方法:

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)

我获得的未使用的变量的错误,当我尝试使用cd以后,它说

c 无法解决

有办法解决吗?

c++

14
推荐指数
3
解决办法
1209
查看次数

围绕返回值的括号 - 为什么?

我经常看到这样的代码(C,C++,有时候是Java):

return (value);
Run Code Online (Sandbox Code Playgroud)

我认为这些括号没有任何好处.所以我的问题是,假设程序员return是某种以返回值作为参数的函数,或者这些括号是否有意义?

我知道这里已经提出了类似的问题,但这只与ANSI C有关.我想知道是否有特定于C++或Java的方面尚未得到解答.

c c++ java

12
推荐指数
2
解决办法
3765
查看次数

为什么"返回(str);" 演绎出不同于"return str"的类型?

情况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)

为什么 …

c++ decltype return-type-deduction c++14

11
推荐指数
1
解决办法
456
查看次数

接受特征密集矩阵和稀疏矩阵的函数

我正在努力将稀疏矩阵支持添加到开源数学库中,并且不希望对矩阵类型DenseSparse矩阵类型都有重复的函数。

下面的示例显示了一个add函数。一个有两个函数的工作示例,然后两次尝试都失败了。下面提供了代码示例的 Godbolt 链接。

我看过了本征文档的编写采取征类型的功能,但其使用的答案Eigen::EigenBase不工作,因为两者MatrixBaseSparseMatrixBase没有提供有特别的方法存在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)

c++ templates multiple-inheritance eigen

8
推荐指数
1
解决办法
365
查看次数

如何在iPhone应用程序上仅支持纵向模式

我正在开发的iPhone应用程序中有一个奇怪的问题.我希望我的应用程序支持纵向模式,但由于某种原因我无法做到(设备和模拟器).

为了仅支持肖像模式我做了如下:

  • 在Xcode的TARGET摘要部分,我只选择了肖像.
  • 我的所有ViewControllers都实现了 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)

我刚刚注意到,当我旋转手机时,我收到以下消息:

"不推荐使用两阶段旋转动画.此应用程序应使用更平滑的单级动画."

这是什么意思?

iphone xcode objective-c autorotate device-orientation

4
推荐指数
3
解决办法
8277
查看次数

C++ nrvo/copy 省略,括号中带有 return 语句

我正在玩弄以下代码,并使用我的 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++ language-lawyer copy-elision nrvo

4
推荐指数
3
解决办法
729
查看次数

decltype(auto)类型推导:return x vs. return(x)

我正在查看有关C ++ 14语言扩展isocpp.org常见问题解答,内容decltype(auto)如下:

...

注意: decltype(auto)如上所示,主要用于推导转发函数和类似包装的返回类型,在此情况下,您希望该类型精确“跟踪”正在调用的某些表达式。但是,除此以外,decltype(auto)并不打算被广泛使用。特别是,尽管它可以用来声明局部变量,但是这样做可能只是一个反模式,因为局部变量的引用性不应该依赖于初始化表达式。另外,它对如何编写return语句也很敏感。这两个函数具有不同的返回类型:

decltype(auto) look_up_a_string_1() { auto str = lookup1(); return str;  }
decltype(auto) look_up_a_string_2() { auto str = lookup1(); return(str); }
Run Code Online (Sandbox Code Playgroud)

第一个返回字符串,第二个返回字符串,string &它是对局部变量的引用str

我的问题:示例中的返回类型是否应该相反,我的意思是,括号应该形成一个表达式,其类型应该是非引用(或右值引用)?并且没有括号,说的str意思是“左值引用str”。我错了吗?

c++ decltype auto c++14 decltype-auto

3
推荐指数
1
解决办法
101
查看次数

为什么 gcc 不检测所有处理的枚举值?

我有一些 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++ gcc gcc-warning clang++

0
推荐指数
1
解决办法
82
查看次数