小编Igo*_*pov的帖子

完美的转发和功能参数绑定的模糊性

我正在尝试C++ 11的完美转发功能.Gnu g ++编译器报告了函数参数绑定的模糊问题(错误显示在下面的源代码之后).我的问题是为什么会这样,因为遵循函数参数绑定过程我没有看到歧义.我的推理如下:在main()中调用tf(a)绑定到tf(int&),因为a是一个左值.然后函数tf将左值引用int&a转发给函数g,因此应该唯一地调用函数void g(int&a).因此,我没有看到模棱两可的原因.重载函数g(int a)时错误消失从代码中删除.这很奇怪,因为g(int a)不能成为用int和a绑定的候选者.

这是我的代码:

void g(int &&a)
{
  a+=30;
}

void g(int &a)
{
  a+=10;
}

void g(int a)   //existence of this function originates the ambiguity issue
{
  a+=20;
}

template<typename T>
void tf(T&& a)
{
  g(forward<T>(a));;
}

int main()
{
  int a=5;
  tf(a);
  cout<<a<<endl;
}
Run Code Online (Sandbox Code Playgroud)

编译g ++ -std = c ++ 11 perfectForwarding.cpp …

c++ c++11

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

Fortran中(/ ... /)的含义是什么?

我不了解Fortran,但必须检查用这种语言编写的代码的一部分.这段代码充满了与此类似的行:

matmul(recVecs2p, real((/ i1, i2, i3 /), dp))
Run Code Online (Sandbox Code Playgroud)

(/ ... /)在Google上找不到语义,所以我希望我能在这里得到答案.

fortran

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

为什么要调用析构函数?

我写了一个简单的向量类,它重载赋值和加法运算符.该代码在添加之前和之后添加两个向量并打印向量.令人惊讶的是,在行c = a + b之后调用析构函数~MyVector().我不明白为什么会这样,因为对象a,b和c都没有超出范围.你了解调用析构函数的原因吗?

#include<iostream>
#include<cstdlib>

using namespace std;

template<typename T>
class MyVector
{
  public:

  MyVector(int s)
  {
    size=s;
    a=new T[s];
  }

  ~MyVector()
  {
    delete[] a;
    size=0;
  };

  void freeVec()
  {
    cout<<"Calling destructor. a[0]= "<<a[0]<<endl;
    if(a!=NULL)
    {
      free(a);
      a=NULL;
    }
  }

  int getSize(void)
  {
    return size;
  }
  void setSize(int ss)
  {
    size=ss;
  }
  T &operator[](int i)
  {
    return a[i];
  }

  MyVector &operator=(MyVector mv)
  {
    if(this==&mv)
      return *this;
    for(int i=0;i<mv.getSize();i++)
      this->a[i]=mv[i];
    this->size=mv.getSize();
    return *this;
  }

  MyVector operator+(MyVector &mv)
  {
    for(int …
Run Code Online (Sandbox Code Playgroud)

c++ destructor

2
推荐指数
1
解决办法
217
查看次数

C++ 概念中的类型要求 (C++20)

我正在使用 g++ 10 学习 C++20 标准的新实现概念。我坚持一个简单的类型要求。即我想实现模板参数T具有T::inner成员名称的要求。这是我的错误代码。这个简单的代码有什么问题以及如何修复它?

#include<concepts>

template<typename T>
concept ContainsInner = requires
{
  typename T::inner;
};

template<ContainsInner T>
struct S{};

struct Q
{
  int inner;
};

int main()
{
  S<Q> s;     // instantiate S with Q for template type, 
              // which must satisfy the ContainsInner concept.
              // Q indeed contains the inner name but still the compilation fails
}
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++20

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

如何从 bash 脚本中删除 root 拥有的文件?

我想从非特权用户拥有的简单脚本中删除 root 用户拥有的文件 (/var/lib/pacman/db.lck):

#!/bin/bash
rm -f /var/lib/pacman/db.lck
Run Code Online (Sandbox Code Playgroud)

但是我不想运行脚本sudo以避免每次我以非特权用户身份执行脚本时都输入密码。为了实现这一点,我设置了s位:

-rwsrwsrwx  1 popov  users       41 04.02.2015 10:35 unlock.sh
Run Code Online (Sandbox Code Playgroud)

但是在运行脚本后我得到

rm: cannot remove ‘/var/lib/pacman/db.lck’: Permission denied
Run Code Online (Sandbox Code Playgroud)

似乎我错误地理解了s位的目的。

所以问题是:如何设置脚本权限(和/或可能是脚本的所有权),这将使脚本在非特权用户调用时删除 root 拥有的文件?

linux bash

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

标签 统计

c++ ×3

bash ×1

c++-concepts ×1

c++11 ×1

c++20 ×1

destructor ×1

fortran ×1

linux ×1