小编dun*_*can的帖子

操作"假<true"是否定义明确?

C++规范是否定义:

  1. 布尔参数的'小于'运算符的存在,如果存在,
  2. 4个参数排列的结果?

换句话说,是否由规范定义的以下操作的结果?

false < false
false < true
true < false
true < true
Run Code Online (Sandbox Code Playgroud)

在我的设置(Centos 7,gcc 4.8.2)中,下面的代码吐出我期望的内容(假设C的历史表示false为0,true为1):

false < false = false
false < true = true
true < false = false
true < true = false
Run Code Online (Sandbox Code Playgroud)

虽然我很确定大多数(所有?)编译器会提供相同的输出,这是否由C++规范立法?或者是一个混淆但符合规范的编译器允许判断true是否小于false?

#include <iostream>

const char * s(bool a)
{
  return (a ? "true" : "false");
}

void test(bool a, bool b)
{
  std::cout << s(a) << " < " << s(b) << " = " << s(a < b) << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ boolean comparison-operators language-lawyer

152
推荐指数
4
解决办法
1万
查看次数

自毁:this-> MyClass :: ~MyClass()vs. this-> ~MyClass()

在我学习C++的过程中,我偶然发现了编写复制构造函数和赋值运算符的文章,该文章提出了一种机制来避免复制构造函数和赋值运算符之间的代码重复.

为了总结/复制该链接的内容,建议的机制是:

struct UtilityClass
{
  ...

  UtilityClass(UtilityClass const &rhs)
    : data_(new int(*rhs_.data_))
  {
    // nothing left to do here
  }

  UtilityClass &operator=(UtilityClass const &rhs)
  {
    //
    // Leaves all the work to the copy constructor.
    //

    if(this != &rhs)
    {
      // deconstruct myself    
      this->UtilityClass::~UtilityClass();

      // reconstruct myself by copying from the right hand side.
      new(this) UtilityClass(rhs);
    }

    return *this;
  }

  ...
};
Run Code Online (Sandbox Code Playgroud)

这似乎是避免代码重复同时确保"编程完整性"的好方法,但需要权衡浪费工作的风险 - 然后分配嵌套内存,而不是重新使用(正如其作者指出的那样).

但我不熟悉其核心语法:

this->UtilityClass::~UtilityClass()
Run Code Online (Sandbox Code Playgroud)

我假设这是一种调用对象的析构函数(破坏对象结构的内容)同时保持结构本身的方法.对于C++新手来说,语法看起来像是对象方法和类方法的奇怪混合.

有人可以向我解释这个语法,还是指向一个可以解释它的资源?

该呼叫与以下内容有何不同?

this->~UtilityClass()
Run Code Online (Sandbox Code Playgroud)

这是合法的电话吗?这是否会破坏对象结构(没有堆;从堆栈中弹出)?

c++ destructor copy-constructor

29
推荐指数
3
解决办法
1679
查看次数

使用auto和decltype从模板化类中的函数返回引用

如何使用auto/decltype强制模板化类中的函数返回对成员变量的引用?

这是我正在尝试做的一个简单的例子.假设您有一个模板化的类,它将某些内容存储在私有成员变量中,a_如下所示:

#include <iostream>

template <typename T>
class A
{
private:
  T a_;

public:
  A(T a) : a_(a) {}

  // 1. Return const reference to a_
  const T & get() const { return a_; }

  // 2. Return non-const reference to a_
  T & get() { return a_; }
};

int main(int argc, char *argv[])
{
  A<int> a(3);

  const auto & a1 = a.get(); // 1. Return const reference to a_
  //a1 = 4;  // Shouldn't compile
  std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ templates decltype auto c++11

13
推荐指数
2
解决办法
5803
查看次数

在C-Visual Studio 2013模板中包装C++成员函数

我正在创建一个轻量级的跨平台插件框架,它在应用程序和插件之间使用C接口(通常但不总是用C++编写).

我帮助C++应用程序和插件编写器的挑战之一是找到一种在C接口上公开C++对象功能的简单方法.我现在的解决方案感觉很简单,并使用模板"构建"C-signature函数,根据这个伟大的stackoverflow问题和答案来包装底层的C++成员函数

template <typename Tc, typename F, F>
struct MemberFuncWrapper;

template <typename Tc,              // C interface structure tag
          typename T,               // C++ class, derived from Tc
          typename R,               // C++ member function return type
          typename ...Args,         // C++ member function argument types
          R (T::*f)(Args...) const> // C++ member function
struct MemberFuncWrapper<Tc, R (T::*)(Args...) const, f> {
  static R call(const Tc * tc, Args... args) {
    const T * t = static_cast<const T *>(tc);
    return ((*t).*f)(args...);
  }
};
Run Code Online (Sandbox Code Playgroud)

该模板的实例化在linux(gcc)和mac(clang)下编译并运行良好,但Visual …

c c++ plugins interface visual-studio-2013

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