C++ 14似乎即将到来,编译器已经在尝试实现这个新版本的核心功能.我正在研究GCC对这些核心功能的支持,并注意到二进制文字部分:GCC实现了它们,但似乎在GNU二进制文字和C++ 14二进制文字之间有所区别.以下是两者的相应参考:
我试图找到他们两个之间的一些差异,因为GCC似乎有所作为,但找不到任何东西.你们中有谁知道更多可能的不兼容性吗?
在Herb Sutter在CppCon16的演讲中,他建议用const std::unique_ptr(约10分钟)写出pimpl习语.
这应该如何与移动构造函数/赋值一起使用?c ++ 17中有什么东西吗?我找不到任何东西.
有没有任何已知的方法来计算两个格雷码的加法(也许是减法),而不必将两个格雷码转换为常规二进制,执行二进制加法然后将结果转换回格雷码?我设法编写递增和递减函数,但加法和减法似乎记录更少,更难写.
我试图让Boost Python与std :: shared_ptr很好地配合.目前,我收到此错误:
Traceback (most recent call last):
File "test.py", line 13, in <module>
comp.place_annotation(circle.centre())
TypeError: No to_python (by-value) converter found for C++ type: std::shared_ptr<cgl::Anchor>
Run Code Online (Sandbox Code Playgroud)
从调用circle.centre(),它返回一个std :: shared_ptr.我可以将每个std :: shared_ptr更改为boost :: shared_ptr(Boost Python可以很好地使用)但是要改变的代码量相当大,我想使用标准库.
circle方法声明如下:
const std::shared_ptr<Anchor> centre() const
{
return Centre;
}
Run Code Online (Sandbox Code Playgroud)
像这样的锚类:
class Anchor
{
Point Where;
Annotation* Parent;
public:
Anchor(Annotation* parent) :
Parent(parent)
{
// Do nothing.
}
void update(const Renderer& renderer)
{
if(Parent)
{
Parent->update(renderer);
}
}
void set(Point point)
{
Where = point;
}
Point …Run Code Online (Sandbox Code Playgroud) 这是本主题的某种后续行动,涉及其中的一小部分.与前一个主题一样,让我们考虑一下我们的编译器有和的constexpr函数.现在,让我们直截了当地说.std::initializer_liststd::array
这有效:
#include <array>
#include <initializer_list>
int main()
{
constexpr std::array<int, 3> a = {{ 1, 2, 3 }};
constexpr int a0 = a[0];
constexpr int a1 = a[1];
constexpr int a2 = a[2];
constexpr std::initializer_list<int> b = { a0, a1, a2 };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不是:
#include <array>
#include <initializer_list>
int main()
{
constexpr std::array<int, 3> a = {{ 1, 2, 3 }};
constexpr std::initializer_list<int> b = { a[0], a[1], a[2] …Run Code Online (Sandbox Code Playgroud) 我最近在玩CRTP的时候遇到的东西让我感到惊讶,因为它与c ++ 1y函数一起使用,其类型是推导出来的.以下代码有效:
template<typename Derived>
struct Base
{
auto foo()
{
return static_cast<Derived*>(this)->foo_impl();
}
};
struct Derived:
public Base<Derived>
{
auto foo_impl()
-> int
{
return 0;
}
};
int main()
{
Derived b;
int i = b.foo();
(void)i;
}
Run Code Online (Sandbox Code Playgroud)
我假设返回类型Base<Derived>::foo是decltype返回的表达式的一个,但如果我修改这样的函数foo:
auto foo()
-> decltype(static_cast<Derived*>(this)->foo_impl())
{
return static_cast<Derived*>(this)->foo_impl();
}
Run Code Online (Sandbox Code Playgroud)
此代码不再起作用,我收到以下错误(来自GCC 4.8.1):
||In instantiation of 'struct Base<Derived>':|
|required from here|
|error: invalid static_cast from type 'Base<Derived>* const' to type 'Derived*'|
||In function 'int main()':|
|error: …Run Code Online (Sandbox Code Playgroud) 我试图转换这样的东西:
if condition?
expression1 line 1
expression1 line 2
expression1 line 3
else
expression2 line 1
end
Run Code Online (Sandbox Code Playgroud)
对于三元组,我的问题是:如何在一行中将多行放入一个表达式?你是否像java中的分号一样分开?像这样?
condition? expression1 line 1; expression1 line 2; expression1 line 3 : expression2
Run Code Online (Sandbox Code Playgroud) 在编写自定义类型特征时,我经常从标准库类型特征派生它们,如下所示:
template<typename T>
struct some_type_trait:
std::is_arithmetic<T>
{};
Run Code Online (Sandbox Code Playgroud)
但是,我有时想知道继承type标准库类型特征的成员类型是否更清晰:
template<typename T>
struct some_type_trait:
std::is_arithmetic<T>::type
{};
Run Code Online (Sandbox Code Playgroud)
总的想法是只有std::bool_constant最终事物的继承,但我们从std::is_arithmetic第一个例子继承而不是直接从std::bool_constant(如第二种情况)继承的事实可以通过多态或类似的实现来观察std::is_base_of.
要点是直接从bool_constantvia type成员类型继承感觉更干净,因为它正是我们想要的.但是,继承的std::is_arithmetic时间稍短,并提供基本相同的行为.那么......在挑选其中一个时,我可能会遗漏任何微妙的优势(正确性,编译时......)?std::is_arithmetic与直接从底层继承相比,是否存在继承可能会改变应用程序行为的微妙场景bool_constant?
以下精简代码不适用于最新的clang ++ 5,但是被g ++ 7接受:
template<typename Wrapped, typename U>
struct wrapper;
template<typename Wrapped, typename U=int>
struct wrapper
{
wrapper() = default;
// Automatic deduction guide
constexpr explicit wrapper(Wrapped) noexcept {}
};
int main()
{
struct {} dummy;
constexpr auto wrapped = wrapper(dummy);
}
Run Code Online (Sandbox Code Playgroud)
它失败并显示以下错误消息:
<source>:18:30: error: no viable constructor or deduction guide for deduction of template arguments of 'wrapper'
constexpr auto wrapped = wrapper(dummy);
^
<source>:12:24: note: candidate template ignored: couldn't infer template argument 'U'
constexpr explicit wrapper(Wrapped) noexcept {} …Run Code Online (Sandbox Code Playgroud) c++ templates language-lawyer template-argument-deduction c++17
我试图创建一个派生自的类,boost::multiprecision::mpz_int并让它继承基类构造函数:
#include <boost/multiprecision/gmp.hpp>
using namespace boost::multiprecision;
struct Integer:
mpz_int
{
using mpz_int::mpz_int;
};
Run Code Online (Sandbox Code Playgroud)
g ++ 4.9.0给出了以下错误:
main.cpp:8:20: error: 'template<class tag, class Arg1, class Arg2, class Arg3, class Arg4> Integer::Integer(const boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>&)' inherited from 'boost::multiprecision::number<boost::multiprecision::backends::gmp_int>'
using mpz_int::mpz_int;
^
main.cpp:8:20: error: conflicts with version inherited from 'boost::multiprecision::number<boost::multiprecision::backends::gmp_int>'
main.cpp:8:20: error: 'template<class Other, boost::multiprecision::expression_template_option ET> Integer::Integer(const boost::multiprecision::number<Backend, ExpressionTemplates>&)' inherited from 'boost::multiprecision::number<boost::multiprecision::backends::gmp_int>'
main.cpp:8:20: error: conflicts with version inherited from 'boost::multiprecision::number<boost::multiprecision::backends::gmp_int>'
main.cpp:8:20: error: 'template<class Other, boost::multiprecision::expression_template_option ET> Integer::Integer(const boost::multiprecision::number<Backend, …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×6
boost ×2
c++14 ×2
c++17 ×2
boost-python ×1
c ×1
compile-time ×1
constexpr ×1
crtp ×1
g++ ×1
gcc ×1
gray-code ×1
literals ×1
ruby ×1
template-argument-deduction ×1
templates ×1
type-traits ×1