为什么std::vector使用自定义分配器移动构造函数不会推断出noexcept()分配器的行为?
这导致封装这种向量的类不能形成可以在某些<algorithm>s中正常移动的(其他)向量.即使基础类型满足nessesary要求(MoveInsertable和DefaultInsertable).
我阅读了JIT接口章节并遇到了问题:如何为最简单的可能代码编写一个最简单的示例(最好是在C++中,至少在x86-64平台上)?说,我想调试以下代码(即code_.data()函数):
#include "eallocator.hpp"
#include <iostream>
#include <vector>
#include <cstdlib>
int main()
{
std::vector< std::uint8_t, eallocator< std::uint8_t > > code_;
code_.push_back(0b11011001u); code_.push_back(0b11101011u); // fldpi
code_.push_back(0b11000011u); // ret
double result_;
__asm("call *%1"
: "=&t"(result_)
: "r"(code_.data())
:
);
std::cout << result_ << std::endl;
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
使用界面我应该做什么(最低限度)?特别是,我希望能够提供一些伪代码(内存中的任意文本)作为"源"(具有相应的行信息),如果可能的话.
如何检测上面的代码(或类似的东西),同时保持简洁.
#include "eallocator.hpp"应该使用针对Windows的方法或针对Linux的方法.
在noexcept函数模板的重载解析期间,说明符括号中的表达式是否参与SFINAE?
我想为聚合创建一个包装器,并希望std::is_constructible谓词正常工作:
template< typename type >
struct embrace
: type
{
template< typename ...arguments >
embrace(arguments &&... _arguments) noexcept(noexcept(type{std::forward< arguments >(_arguments)...}))
: type{std::forward< arguments >(_arguments)...} // braces
{ ; }
};
int
main()
{
struct S { int i; double j; }; // aggregate
using E = embrace< S >;
E b(1, 1.0); // "parentheses"-constructible => can be used as usual types
b.i = 1; b.j = 2.0; // accessible
static_assert(std::is_constructible< E, int, double >{});
static_assert(std::is_constructible< …Run Code Online (Sandbox Code Playgroud) 在技术上有效的是使用不匹配的特化std::allocator(当然,除了它的专业化void)作为STL容器的模板参数(不是全部,但下面列举加上无序_(多)映射/集)?以下代码编译正常.
#include <list>
#include <forward_list>
#include <deque>
#include <set>
#include <map>
int main()
{
struct A { bool operator < (A) const { return true; } };
struct B {};
struct C {};
std::list< A, std::allocator< C > > l;
std::forward_list< A, std::allocator< C > > fl;
std::deque< A, std::allocator< C > > d;
std::set< A, std::less< A >, std::allocator< C > > s;
std::multiset< A, std::less< A >, std::allocator< C > > ms;
std::map< A, …Run Code Online (Sandbox Code Playgroud) 将标记类型定义为匿名空结构或空结构之间的用法是否有任何区别?
using A = struct {};
struct B {};
Run Code Online (Sandbox Code Playgroud)
在我的脑海里,唯一的区别是"有效的"类型名,当一个人使用的一种反射(即__PRETTY_FUNCTION__,<cxxabi.h>:abi::__cxa_demangle(typeid().name())等).
ADL适用于两种方式:
namespace ns
{
using A = struct {};
struct B {};
constexpr
bool
adl(A)
{
return true;
}
constexpr
bool
adl(B)
{
return true;
}
}
template< typename type >
constexpr
bool
adl(type)
{
return false;
}
static_assert(adl(ns::A{}));
static_assert(adl(ns::B{}));
Run Code Online (Sandbox Code Playgroud) 如何禁止构造物体?我将= delete;所有相关的特殊功能标记如下:
struct A
{
A() = delete;
A(A const &) = delete;
A(A &&) = delete;
void * operator new(std::size_t) = delete;
void operator delete(void *) = delete;
};
A x{};
A y = {};
A * z = ::new A{};
Run Code Online (Sandbox Code Playgroud)
但是x,y和*z仍然可以存在.该怎么办?我对这两种情况都感兴趣; 静态/堆栈分配和堆分配.
当用G ++(gcc 4.8.1和MinGW 4.8.2 with -std=gnu++1yflag)编译我的代码时,我发现了一个奇怪的行为.在SSCCE的精神中,我隔离了以下代码段:
struct C
{
template< typename X >
auto
f(X &&) const &
{ ; }
template< typename X >
auto
f(X &&) &
{ ; }
template< typename X >
auto
f(X &&) &&
{ ; }
};
int main()
{
int i{};
#if 1
C{}.f(i);
#endif
#if 1
C c{};
c.f(i);
#endif
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误:
main.cpp: In function 'int main()':
main.cpp:29:10: error: call of overloaded …Run Code Online (Sandbox Code Playgroud) 为什么在C++中带有不同名称的两个函数ref和cref?为什么不是唯一的重载ref函数?是否有一些重要的语义原因?
在C++中编写类似库的代码我发现在copy_cv_reference_t类型特征中特别需要:
struct A;
struct B;
static_assert(std::is_same< copy_cv_reference_t< A , B >, B >{});
static_assert(std::is_same< copy_cv_reference_t< A const , B >, B const >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A , B >, volatile B >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A const , B >, volatile B const >{});
static_assert(std::is_same< copy_cv_reference_t< A &, B >, B & >{});
static_assert(std::is_same< copy_cv_reference_t< A const &, B >, B const & >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A &, B >, volatile B & >{}); …Run Code Online (Sandbox Code Playgroud) 我想为用Haskell(GHC)编写的代码提供回调函数.它使用类似GCC C编译器的函数类型来导出/导入功能,并在运行时与我的代码进行互操作.
我必须提供一个回调函数,它实际上接受this指向该类的指针并只调用它的方法:
struct C
{
int f(int i) { ; }
static int f_callback(void * self, int i)
{
static_cast< C * >(self)->f(i);
}
};
Run Code Online (Sandbox Code Playgroud)
逻辑上f_callback是类的一部分C,因此我将它放入相应的命名空间范围.
但我担心我应该使用extern "C"语言规范(调用约定在这里是重要的,而不是名称错误)?这是可能的声明和定义extern "C"在平原命名空间功能,有一对夫妇的特殊规则,extern "C"在不同的命名空间具有相同的名称定义的功能,但有类范围的命名空间和简单的一个命名空间之间没有区别.
是否可以将static extern "C"函数定义到类范围?