小编xml*_*lmx的帖子

这三个默认构造函数在 C++ 中等效吗?

考虑以下代码:

#include <type_traits>

template<typename T>
struct A1 {
    T t;

    // implicitly-declared default constructor
};

template<typename T>
struct A2 {
    T t;

    // explicitly-declared default constructor without noexcept
    A2() = default;
};

template<typename T>
struct A3 {
    T t;

    // explicitly-declared default constructor with noexcept
    A3() noexcept(std::is_nothrow_default_constructible<T>::value) = default;
};
Run Code Online (Sandbox Code Playgroud)

这三个默认构造函数在 C++ 中等效吗?

c++ standards class default-constructor noexcept

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

VC++ 2010的另一个BUG?关于在标头中声明常量REFERENCE

几行代码值得千言万语:

我有三个简单的文件:header.h,main.cpp,other.cpp

// header.h

  #pragma once

inline const int& GetConst()
{
    static int n = 0;
    return n;
}

const int& r = GetConst();

// main.cpp

  #include "header.h"

int main()
{
    return 0;
}

// other.cpp

  #include "header.h"
Run Code Online (Sandbox Code Playgroud)

在编译最简单的项目时,VC++ 2010抱怨如下:

ClCompile:
  other.cpp
  main.cpp
  Generating Code...
  other.obj : error LNK2005: "int const & const r" (?r@@3ABHB) already defined in main.obj
D:\Test\Debug\bug.exe : fatal error LNK1169: one or more multiply defined symbols found

Build FAILED.

Time Elapsed 00:00:00.29
========== Build: 0 succeeded, …
Run Code Online (Sandbox Code Playgroud)

c++ const reference linkage visual-c++

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

将函数声明为BigStruct && foo(...)是一种好习惯吗?

class A
{
public:
    A()
    {
        cout << "A()" << endl;
    }

    A(const A&)
    {
        cout << "A(const A&)" << endl;
    }

    A(A&&)
    {
        cout << "A(A&&)" << endl;
    }

    A& operator=(const A&)
    {
        cout << "A(const A&)" << endl;
    }

    A& operator=(A&&)
    {
        cout << "A(const A&&)" << endl;
    }

    ~A()
    {
        cout << "~A()" << endl;
    }
};

A&& f_1()
{
    A a;
    return static_cast<A&&>(a);
}

A f_2()
{
    A a;
    return static_cast<A&&>(a);
}

int main()
{
    cout …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue

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

"by bytes"vs"in bytes"

template<class CharType>
struct StringWithLength
{
    size_t    length;
    CharType* str_buf;
};
Run Code Online (Sandbox Code Playgroud)

我想在字段长度上添加一些注释.我有两个选择:

#1. "The field length is the size of str_buf by the byte" 
    (Consider "The worker is paid by the hour")

#2. "The field length is the size of str_buf in bytes"
Run Code Online (Sandbox Code Playgroud)

从英语母语人士的角度来看哪个更自然?

谢谢.

c++ comments coding-style

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

RAII应该导致内存泄漏吗?

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

struct A
{
    A(char* p)
        : p(p)
    {}

    ~A()
    {   
        delete this->p;
    }

    char* p;
};

int main()
{
    A a(new char);
    _CrtDumpMemoryLeaks();
}
Run Code Online (Sandbox Code Playgroud)

在调试模式下运行后,Visual Studio 2012的输出窗口显示:

Detected memory leaks!
Dumping objects ->
{142} normal block at 0x007395A8, 1 bytes long.
 Data: < > CD 
Object dump complete.
Run Code Online (Sandbox Code Playgroud)

原因是什么?

c++ memory-leaks raii visual-c++

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

是吗?:表达式并不总是被评估?

在Lisp中,可以评估任何表达式.C++采用的概念是:"表达式","价值","评估".

如果您不知道"表达式","值"和"评估"之间的关系,请参阅C++标准5.1.

我知道?:表达式与+表达式相同.

必须能够评估任何表达式并给出值.然而,?:表达似乎并非总是如此.

void f1() {}
void f2() {}

void test(bool b)
{
    b ? f1() : f2(); // OK. What's the value of this expression?
}
Run Code Online (Sandbox Code Playgroud)

任何表达式都应该有一个值; b ? f1() : f2();是一种表达; 它的价值是什么?

任何解释?

更新和我自己的答案:

摘自C++标准5.1:

表达式可能会导致值,并可能导致副作用.

c c++ evaluation expression ternary-operator

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

编译最简单的代码时VC++编译器崩溃

我的编译器是VC++ 2013和2013 Novmember CTP.

以下代码使VC++编译器崩溃并报告:

"致命错误C1001:编译器中发生内部错误."

template<class T>
class A
{
    operator T*() const
    {
        return p;
    }

    T* p;
};

template<class T>
class B : public A<T>
{
    using A::operator T*;
};

int main()
{}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction visual-studio visual-c++ c++11

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

为什么grep给出"二进制文件(标准输入)匹配"?

#include <stdio.h>

int main()
{
    FILE* cmd = popen("grep Hello", "w");   
    fwrite("Hello\n", 6, 6, cmd);
    fwrite("Hillo\n", 6, 6, cmd);
    fwrite("Hello\n", 6, 6, cmd);   
    pclose(cmd);
}
Run Code Online (Sandbox Code Playgroud)

上述程序输出:

二进制文件(标准输入)匹配

为什么grep会给出消息,以及如何修复它?

c api io file pipe

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

为什么模板模板参数不能按预期工作?

#include <vector>

template
<
    typename T,
    typename Alloc,
    template<typename, typename> class Left
>
Left<T, Alloc>&&
operator <<(Left<T, Alloc>&& coll, T&& value)
{
    coll.push_back(std::forward<T>(value));
    return std::forward<Left<T, Alloc>>(coll);
}

using namespace std;

int main()
{
    vector<int> c1;
    c1 << int(8);
}
Run Code Online (Sandbox Code Playgroud)

VS 2015年产量:

错误C2678:二进制'<<':找不到运算符,它采用类型'std :: vector>'的左手操作数(或者没有可接受的转换)

为什么模板模板参数不能按预期工作?

c++ templates compiler-errors c++11

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

传递给std :: runtime_error的ctor的字符串对象的生命周期是多少?

根据cppreferences,explicit runtime_error( const std::string& what_arg );不会复制what_arg的内容.

我可以安全地将临时字符串对象传递给std::runtime_error's ctor

例如:

std::string GetATempString(const char* msg)
{
    return { msg };
}

int main()
{
    try {
        throw std::runtime_error(GetATempString("Hello"));
    } catch (const std::runtime_error& e) 
    {
            e.what(); // Is it guaranteed that "Hello" would be returned safely?
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ standards exception object-lifetime c++11

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