小编asm*_*mmo的帖子

使用一个函数调用 C++ 初始化多个常量类成员

如果我有两个不同的常量成员变量,它们都需要基于相同的函数调用进行初始化,有没有办法在不调用函数两次的情况下做到这一点?

例如,分子和分母是常数的分数类。

int gcd(int a, int b); // Greatest Common Divisor
class Fraction {
public:
    // Lets say we want to initialize to a reduced fraction
    Fraction(int a, int b) : numerator(a/gcd(a,b)), denominator(b/gcd(a,b))
    {

    }
private:
    const int numerator, denominator;
};
Run Code Online (Sandbox Code Playgroud)

这会浪费时间,因为 GCD 函数被调用了两次。您还可以定义一个新的类成员 ,gcd_a_b然后首先将 gcd 的输出分配给初始化列表中的那个,但这会导致浪费内存。

一般来说,有没有办法在不浪费函数调用或内存的情况下做到这一点?您可以在初始化列表中创建临时变量吗?

c++ oop constructor constants initializer-list

52
推荐指数
2
解决办法
2365
查看次数

用在别处创建的对象填充 std::vector

我正在尝试std::vector使用在函数中创建的对象填充 a ,如下所示:

class Foo
{
public:
   Foo() { std::cout << "Foo created!\n"; }
   Foo(const Foo& other) { std::cout << "Foo copied!\n"; }
   Foo(Foo&& other) { std::cout << "Foo moved\n"; }
   ~Foo() { std::cout << "Foo destroyed\n"; }
};

static Foo createFoo() 
{
   return Foo();
}

int main()
{
   {
       std::vector<Foo> fooVector;
       fooVector.reserve(2);
       fooVector.push_back(createFoo());
       fooVector.push_back(createFoo());
       std::cout << "reaching end of scope\n";
   }
   std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)

输出:

Foo created!
Foo moved
Foo destroyed
Foo created!
Foo moved
Foo destroyed
reaching …
Run Code Online (Sandbox Code Playgroud)

c++ vector move-semantics

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

我如何检测除了整数之外的任何东西是否被传递给我的类构造函数?

公平而简单:如何检查除整数之外的其他任何内容是否已在 C++ 中传递给我的类?

如果我通过 fe achar 'a'我的班级会得到编号97以 ascii 形式。

我试过了,std::numeric_limits但我不明白为什么它没有检测整数:

#include <iostream>
#include <limits>

class integerCheck
{
public:
    integerCheck(int value)
    {
       if((value != std::numeric_limits<int>::is_integer))
           return;

       std::cout << value << std::endl;
    }
};

int main()
{
    integerCheck valInt(88);
    integerCheck valChar('a');
    integerCheck valFloat(13.44f);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我发现这篇文章std::enable_if可以使用,但我无法想象即使在 c++20 中也无法检测到错误的输入,而是将所有内容都包装在模板中。

我错过了什么,我应该寻找/搜索什么来检测除整数值之外的任何东西? 预先感谢

c++ oop int numeric-limits c++20

9
推荐指数
2
解决办法
166
查看次数

constexpr for 循环编译

我已经读过这篇文章,但我仍然不知道如何使其工作,-std=gnu++2a 我不知道如何使用integer seq。您能帮我修改下面的代码以便它可以编译吗?谢谢

constexpr bool example(const int k)
{
    return k < 23 ? true: false;
}

constexpr bool looper()
{
    constexpr bool result = false;
    for(int k = 0; k < 20; k ++)
    {
        for (int i = 0 ; i < k; ++i)
        {
        constexpr bool result = example(i);
        }
    }

    return result;
}

int main()
{
    constexpr bool result = looper();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ metaprogramming constexpr c++11 c++17

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

我可以禁止临时对象作为参数吗?

假设我有这个功能:

void foo(Object& o) {
    /* only query o, dont alter it*/
}
Run Code Online (Sandbox Code Playgroud)

是否可以仅使用已构造的对象调用此函数,并且如果我使用临时对象调用该函数,Visual Studio 会抛出编译错误?

struct Object {
    /*Members*/
}

void foo(Object& o) {
    /* only query o, dont alter it*/
}

int main() {
    Object o = Object();
    foo(o); // allow this
    foo(Object()) // but disallow this
}
Run Code Online (Sandbox Code Playgroud)

c++ function visual-studio move-semantics pass-by-rvalue-reference

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

std::initializer_list 堆是否分配内存?

简单的问题,std::initializer_list 堆是否分配内存?我不是在谈论它的元素项,只是在谈论存储元素的缓冲区本身。

c++ memory-management initializer-list c++11

4
推荐指数
2
解决办法
521
查看次数

C++ std::reduce 与数组

int arr1[5] = { 1,2,3,4,5 };
int sum = reduce(arr1[0], arr1[5]);
Run Code Online (Sandbox Code Playgroud)

我编写此代码是为了将该std::reduce函数与整数数组一起使用。我应该如何定义数组的开头和结尾?

c++ iterator stl std

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

在 C++ 中将集合的前 n 个值复制到向量中的简单方法

简单的问题,假设我保存了一个包含 5 个值的集合,并且我想将前 3 个值复制到一个向量中,有没有一种快速简单的方法可以做到这一点?我知道,如果我想将整个集合复制到向量中,我可以使用如下所示的内容:

set <int> test = {0,1,2,3,4,5};
vector<int> vect(test.begin(),test.end());
Run Code Online (Sandbox Code Playgroud)

我可以做类似只复制前 n 个值的事情吗?我一直在使用 for 循环来执行此操作,但我只是想检查是否有更快的编码方法。

c++ iterator stl c++14 c++17

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

为什么最大两个字符串文字的输出是错误的?

有人可以解释一下,为什么这段代码中的输出是“C”?

#include <iostream>
using namespace std;
template<class X>
X maximum(X a,X b)
{
    if(a > b)
        return a;
    else 
        return b;
}

int main() {
    cout << maximum("C","D") << endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates string-literals

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

初始化函数在 `lazy_static` 块中被调用两次

我有一个大项目,我lazy_static用来创建一个singleton. 我认为lazy_staticcrate 中存在错误(仅出现在大型项目中)或者我做错了什么,因为必须调用一次以创建单例的初始化函数被调用两次。

项目结构如下


Foo
|__foo-core
|  |__src
|  |  |__lib.rs
|  |__Cargo.toml
|
|__foo-high
|  |__src
|  |  |__lib.rs
|  |__Cargo.toml
|
|__src
|  |__lib.rs
|__Cargo.toml
Run Code Online (Sandbox Code Playgroud)

foo/foo-core/src/lib.rs

pub mod my_file {
    pub struct MyFile {
        file: std::fs::File,
    }

    impl MyFile {
        pub fn open(
            path: &'static str,
        ) -> Result<MyFile, Box<dyn std::error::Error + Send + Sync>> {
            let file_ = std::fs::File::create(path)?;
            Ok(MyFile { file: file_ })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

foo/foo-high/src/lib.rs

mod high { …
Run Code Online (Sandbox Code Playgroud)

singleton rust lazy-static

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