标签: c++17

在c ++ 17中,struct auto定义的构造函数有哪些规则?

鉴于:

struct X {
  int m;
  std::string s;
};
Run Code Online (Sandbox Code Playgroud)

我可以:

X x;  // invokes automatically defined default ctor
X y = { 5 };   // invokes whatever became of the original struct initialization but now maybe runs through C++ initializer-lists?
X z = { 5, "yolo" };  // I assume this is an initializer-list that is being handled by some rule for structs that either runs through a compiler created ctor or copy-from-initializer-list that is similarly compiler-created
Run Code Online (Sandbox Code Playgroud)

乃至...

std::vector<X> vx; …
Run Code Online (Sandbox Code Playgroud)

c++ struct c++17

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

no除了不被腐烂去除

为什么不std::decay从函数指针中删除noexcept说明符?

例如,这符合c ++ 17

#include <type_traits>

template<typename>
struct is_noexcept {};

template<typename R, typename ... Arg>
struct is_noexcept<R(*)(Arg...)> : public std::false_type {};

template<typename R, typename ... Arg>
struct is_noexcept<R(*)(Arg...) noexcept> : public std::true_type {};

void test() noexcept {}

int main(){
    void (*b)() noexcept = test;
    static_assert(is_noexcept<decltype(b)>::value);
    static_assert(is_noexcept<std::decay<decltype(b)>::type>::value);
}
Run Code Online (Sandbox Code Playgroud)

c++ c++17

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

静态或动态类型用于"sizeof expr"?

是静态还是动态类型的expr使用sizeof expr

请引用C++ 17标准.

c++ static-typing sizeof language-lawyer c++17

0
推荐指数
2
解决办法
165
查看次数

如何在c ++ 17中获取存储在"any"中的数据大小?

假设我有这样的功能

int writetofile(wstring name, any sdata){
  ...
   return error;
}
Run Code Online (Sandbox Code Playgroud)

此函数不知道将存储哪些数据,但需要知道存储的数据的大小sdata.虽然很容易确定存储在其中的数据类型,sdata但我认为没有一些简单的方法可以了解数据的大小sdata.


我有一个具有类型成员的数据结构wstring.现在我们不能将该数据结构直接写入文件,因为它包含wstring.据我研究互联网上,最好的方式写wstring或者string是写尺寸的第一字符串,然后该字符串.然后当我读取字符串时首先读取大小然后读取那么多的大小.

为此,我已经发挥了作用.

int filemanager::write(any data, fileid uid, DWORD *byteswritten) const
{
    // files is a map<fileid, fileinfo> where fileinfo is a struct which has 
    // members including file's name and handle 
    // fileid is a typedef of int
    if (!files.count(uid)) return -1;
    if (!data.has_value()) return -2;
    if (data.type() == typeid(wstring)) {
        DWORD sz1, sz2;
        wstring str = …
Run Code Online (Sandbox Code Playgroud)

c++ sizeof c++17

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

if(abs(num-num2)<= 0.01)cout <<"XYyx"; 不能用c ++工作

我是新手程序员.使用c ++控制台程序,我想计算两个数字是否几乎相等(2个数字之间的差值必须介于0.01之间.当2个数字为317.99时,该程序给出正确的结果.但是当数字超出此范围时它不起作用.这是代码: -

double num,num2;
while(cin>>num>>num2)
{
    if(abs(num-num2)<=0.01)
    {
       cout<<"The numbers are almost equal";
    }
} 
Run Code Online (Sandbox Code Playgroud)

而且我也尝试过这种方式: -

double num,num2;
while(cin>>num>>num2)
{
    if(num>num2)
    {
        if(num-num2<=0.01)
            cout<<"~";
    }
    else if(num2>num)
    {
        if(num2-num<=0.01)
            cout<<"~";
    }
} 
Run Code Online (Sandbox Code Playgroud)

它不适用于这两种方式.我用(17.99,17.88),(12.34,12.35),(3.01,3.00),(18.00,17.99),(2.99,3.00)测试了这个,依此类推.正如我之前所说,它只适用于[3,18]范围内的那些数字.但是,当我尝试使用此代码时: -

double num,num2;
while(cin>>num>>num2)
{
   if(num>num2)
   {
      if(num2+0.01>=num)
      {
         cout<<"~";
      }
   }
   else if(num2>num)
   {
      if(num+0.01>=num)
      {
         cout<<"~";
      }
   }
} 
Run Code Online (Sandbox Code Playgroud)

有效.但正如我们在数学上所知: -

num-num2<=0.01  Or, -num2-0.01<=-num  Or, num2+0.01>=num 
Run Code Online (Sandbox Code Playgroud)

因此num-num2 <= 0.01与说num2 + 0.01> = …

c++ c++17

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

转发参数包到构造函数()在g ++ 6.2.1中失败

如何在g ++ - 6.2.1中克服/解决此错误

以下代码适用于g ++ - 7.3.0,但升级编译器对我来说不是一个选项.所以我正在寻找一些SFINAE魔法......尝试很少但到目前为止失败了......

class Base {
public:
  Base(std::string str) : s(std::make_shared<std::string>(str)) {}
  Base(Base &&base) noexcept { s = std::move(base.s); }
  Base &operator=(Base &&i_base_actor) noexcept {
      s = std::move(i_base_actor.s);
      return *this;
  }
  virtual ~Base() = default;

private:
  std::shared_ptr<std::string> s;
};

// Derived
class Derived : public Base {
public:
   Derived() :Base("Derived") {}
   ~Derived() = default;
};

// Derived1
class Derived1 : public Base {
public:
   Derived1(int a) :Base("Derived1") {}
   ~Derived1() = default;
};
Run Code Online (Sandbox Code Playgroud)

包装功能:

template<typename T, …
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics variadic-templates perfect-forwarding c++17

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

基于派生类型编译时间行为分支

我有一对类FS和DS,它们都来自另一个类S.我有一个函数,我传入一个S*(即FS或DS),它进一步调用另一个函数传入相同的S*.在这个最终函数中,我需要根据S是FS还是DS来具有不同的行为,但是由于它在关键路径上,所以希望在编译时解析该分支.我怎样才能做到这一点?涉及元编程和/或constexpr的东西我觉得 - 我不想为每个传递进行虚拟调用

为简洁起见,省略了无关的细节

class S {};
class FS : public S {};
class DS : public S {};

void func_2(Space *s) {
     // common code
     // branch based on lowest type of s
     // common code
}

void func_1(Space *s) {
    // common code
    func_2(s);
}
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism template-meta-programming c++17

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

如何将structure/union数组的成员传递给函数

例如,我想显示结构/联合数组的一个成员的值,所以我想将它作为参数传递给一个只显示一个成员的函数,并且还将显示我作为参数传递的任何其他单个成员.

#include <iostream>
using namespace std;

union ThreeTypes
{
    char letter;
    int whole;
    double real;
};

void showArr(ThreeTypes[], int); // ?? What parameters to pass?

int main()
{
    const int SIZE = 50;
    ThreeTypes arr[SIZE];
    for (int i = 0; i < SIZE; i++)
        arr[i].real = 2.37;

    showArr(arr, SIZE, ??? ); // what argument to pass to display member?

    return 0;
}
void showArr(ThreeTypes arr[],int size,???) // also, what parameters??
{
    for (int i = 0; i < size; i++) …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

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

C++:在不使用反斜杠(\)的情况下考虑C++ std :: string中双引号(")的字面含义

我收到了以字符串格式收到的JSON数据.

std::string jsonStr = GetJsonStringBuffer();
Run Code Online (Sandbox Code Playgroud)

//它可能会像这样存储在jsonStr中:

jsonStr = " {"Key1":"val1","key2":"val2","key3":"val3"} " ;

BUT JSON字符串本身有双引号(").所以当GetJsonStringBuffer()返回一个C++ std :: string存储时jsonStr.它不会产生问题,因为带有json数据的双引号(")可能被视为字符串分隔符C++ std :: string.

如何处理这种情况.我是否以编程方式为json字符串中的每个双引号添加"\"(反斜杠),我认为这不是一个很好的解决方案.有没有更好的C++解决方案呢?

c++ string stdstring visual-c++ c++17

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

如何在C ++ 17中移动字符串的字母?

例如,通过在字母表中向前移动两个字母来从“ Hi”转换为“ Jk”。

到目前为止,我已经尝试过了:

string myString = 'Hello';
string shifted = myString + 2;
cout << shifted << endl;
Run Code Online (Sandbox Code Playgroud)

这适用于字符,但不会对字符串做任何事情。还有其他有用的方法吗?

c++ c++17

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