小编Mil*_*nek的帖子

如何在编译时进行条件变量初始化?

C++ 11标准具有std::conditional<>用于在编译器时通过某种布尔条件选择类型的模板.如何进行相同的操作但是为变量初始化选择init值?类似于type a = (exp) ? first_value : second_value;.

我用我的模板:

template<bool B, typename T>
inline constexpr T&& conditional_initialize(T&& i1, T&& i2) {
    return B ? std::move(i1) : std::move(i2);
}
Run Code Online (Sandbox Code Playgroud)

但它只能用于POD类型:int a = conditional_initialize<true>(1, 2);.对于数组初始化,此模板编译时出错.错误的编译示例:int a[] = conditional_initialize<true>({1, 2}, {3,4,5});

错误消息:没有匹配函数用于调用'conditional_initialize(<brace-enclosed initializer list>,<brace-enclosed initializer list>)';

谁可以帮我模板?

c++ c++11

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

C++ 警告:将新创建的 gsl::owner&lt;&gt; 分配给非所有者

当我使用以下代码时,我收到一条警告(来自应用 cppcoreguideline)。代码:

 SampleClass *object = nullptr;
 object = new SampleClass();
Run Code Online (Sandbox Code Playgroud)

警告:

 warning: assigning newly created 'gsl::owner<>' to non-owner 'SampleClass *' [cppcoreguidelines-owning-memory]
Run Code Online (Sandbox Code Playgroud)

当我搜索并访问此链接时 https://releases.llvm.org/8.0.1/tools/clang/tools/extra/docs/clang-tidy/checks/cppcoreguidelines-owning-memory.html

我无法理解,有人可以用简单的术语解释一下。

c++ cpp-core-guidelines

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

(C ++)无法解析INT_MAX和INT_MIN?

我目前正在www.udemy.com上参加C ++课程。在其中一堂课上,老师正在#include <limits>用来演示整数可以拥有的最大整数数。但是,当我要包括限制时,一切都会顺利进行,直到我尝试打印INT_MAX和INT_MIN来查看最大整数和最小整数。这是我的代码:

#include <iostream>
#include <limits>

using namespace std;

int main() {

    cout << "MaxInt= " << INT_MAX << endl;
    cout << "MinInt = " << INT_MIN << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

旁注:我正在Windows 10和MinGW编译器上使用Eclipse Neon CDT。

c++

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

为什么这是使用指向c ++的输出

我不知道为什么这段代码会给出结果.我认为指针p应该是地址,str输出应该始终是while循环中的地址.为什么结果是那样的真正价值str

char str[] = "we are poor students";//???????
cout<<str<<endl;
char *p = str;
while (*p != '\0')
{
    cout << p<<endl;
    p++;
}
return 0;
Run Code Online (Sandbox Code Playgroud)

这是它给出的结果:

we are poor students
we are poor students
e are poor students
 are poor students
are poor students
re poor students
e poor students
 poor students
poor students
oor students
or students
r students
 students
students
tudents
udents
dents
ents
nts
ts
s
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

初始化参数时会发生什么?C++

void foo (int i , int k = 7) {
    cout << k;
}

int main(){
    foo(1, 2);    
}
Run Code Online (Sandbox Code Playgroud)

k将输出2.我的问题是,foo以什么顺序初始化参数并获取参数?什么是foo经历的过程2.谢谢

c++ parameters

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

模板类实例之间的类型转换

我目前正在尝试使用c ++样式类型转换。为此,我创建了一个模板类pClass,它接受一些T类型的元素并将其打印出来。

现在,我想(例如)将type的实例转换pClass<char>pClass<int>。但是,非我的类型转换尝试似乎按预期方式工作。

我已经发现它dynamic_cast用于在运行时以及处理虚拟函数/多态类时进行转换,在此情况并非如此。static_cast用于在编译时进行转换。那么,对于我来说,static_cast应该是正确的选择吗?

这里有关stackoverflow的一些主题也有类似的问题,但是仅当处理多个类并在它们之间进行继承时才有类似的问题。不幸的是,我无法真正将它们与我的问题联系起来(例如,模板调用之间的C ++ Casting)。

#include <iostream>

template <typename T>
class pClass {
    public:
    T value;
    pClass(T value) {
        this->value = value;
        std::cout << value << std::endl;
    }
    virtual ~pClass() {}
};

int main() {
    pClass<int> pInt(5);
    pClass<char> pChar('a');
    pClass<float> pFloat(4.2f);

    // pClass<int> pInt2 = static_cast<pClass<int>&>(pChar); //  gives invalid type conversation
    // pClass<int>& pInt3 = dynamic_cast<pClass<int>&>(pChar); // warning: dynamic_cast of ‘pClass<char> pChar’ to ‘class pClass<int>&’ can …
Run Code Online (Sandbox Code Playgroud)

c++ templates casting type-conversion

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

endl 是否有必要刷新缓冲区?

根据 的定义endl,它用于插入换行符并刷新流。我记得如果插入新行,则缓冲区将自动刷新。如果是这样,为什么endl在插入新行后仍然需要刷新流。

c++ flush

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

C++ 错误 C2280。为什么结构体不可复制

我在 C 项目中的头文件中有一个结构。

struct tgMethod {
    const char *name;
    const enum tgAccessModifier access;
    const enum tgMethodKind kind;
    tgTypeRef return_type;
    const tgParams params;
    const tgMethod *overrides;
    const void *userptr;
    const tgObject *(*methodptr)(tgObject *, size_t, tgObject *, void *);
};
Run Code Online (Sandbox Code Playgroud)

在链接这个 C 项目的 C++ 项目中,我有这个结构,它使用 asEntryAllocator<tgMethod>但编译器给出错误:error C2280: "tgMethod &tgMethod::operator =(const tgMethod &)": attempting to reference a deleted function

template<typename T>
struct EntryAllocator {
public:
    EntryAllocator() : EntryAllocator(1 << 7) { }

    explicit EntryAllocator(size_t max_size) :
                _start(0), _count(0), _capacity(max_size), _data((T*)calloc(max_size, …
Run Code Online (Sandbox Code Playgroud)

c++ struct copy-constructor

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