#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
函数fun()通过引用返回值,但是在main()方法中我将一些int分配给函数.理想情况下,编译器应该显示像左值所需的错误但在上面的情况下程序工作正常.为什么会这样?
我需要一些内存管理,并希望我可以将它建立在一些std容器上.我的要求是:
所以,我需要一些可以通过添加块来扩展的东西,比如std::deque.但是std::deque,我不能保证通过8个元素进行扩展会给我一个连续的块.而且std::deque没有,capacity所以我无法"适应" std::deque.
这意味着我必须自己写,对吗?(注意:我不想知道如何写我自己的,但只有我必须).
编辑澄清:只有在每次扩展时获得的元素块必须是连续的,而不是整个容器 - 这显然与其他要求相矛盾.
编辑为jalf所以这是什么:用于"排序"3D点的空间八度树.树节点指的是立方体单元,并形成与使用指针链接的父母和女儿的链接结构.同级节点未链接,但在内存中相邻.事先不知道节点的总数(因为每个最终节点的点数> 1),但是可以获得估计.在树构建期间,当划分非最终节点时,必须获得最多8个新节点的连续块,然后将其链接到树中.移动或复制这些节点会使任何现有链接(指针)无效.
另一个编辑只是为了澄清一些讨论.任何基于的设计都std::vector<T>不得使用resize()和/或reserve().两者都需要T在某些条件下复制或移动构造函数.即使从未在这些条件下调用过,代码也无法编译.
今天我非常惊讶地发现英特尔的icpc(版本14.0.2,使用std=c++0x)无法编译以下代码段.
#include <type_traits>
namespace traits_tests {
template<typename>
struct sfinae_true : std::true_type {};
template<typename T>
static auto value_type(int) -> sfinae_true<typename T::value_type>;
template<typename T>
static auto value_type(void*) -> std::false_type;
}
template<typename C>
struct has_value_type
: decltype(traits_tests::value_type<C>(0)) {};
Run Code Online (Sandbox Code Playgroud)
抱怨最后一行:
inc/traits.h(258): error: expected an identifier
: decltype(traits_tests::value_type<C>(0)) {};
^
Run Code Online (Sandbox Code Playgroud)
代码适用于clang和gcc.
我真的不想完全重写,以使它与有缺陷的编译器一起工作(为什么商业编译器总是有缺陷?).
icc?编辑:是的,我知道icc支持decltype一段时间以来.但在上述特定背景下,icc未能支持它.另请注意,使用std=c++11而不是std=c++0x没有区别.
我正在尝试boost::multiprecision::float128在C++ 11(gcc 4.8.1)下使用(boost 1.55.0),但得到以下编译器错误:
/cm/shared/apps/boost/gcc/1.55.0/include/boost/multiprecision/float128.hpp: In static member function ‘static std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ET> >::number_type std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ET> >::min()’:
/cm/shared/apps/boost/gcc/1.55.0/include/boost/multiprecision/float128.hpp:533:55: error: unable to find numeric literal operator ‘operator"" Q’
static number_type (min)() BOOST_NOEXCEPT { return 3.36210314311209350626267781732175260e-4932Q; }
Run Code Online (Sandbox Code Playgroud)
我不能boost::multiprecision::float128在C++ 11中使用?或者我怎么让它运作?
编辑
只是为了澄清.此错误由生成
#include <boost/multiprecision/float128.hpp>
Run Code Online (Sandbox Code Playgroud)
编译器对该语句不满意
return 3.36210314311209350626267781732175260e-4932Q;
Run Code Online (Sandbox Code Playgroud)
特别Q是它令人困惑.我使用了编译器标志-std=c++11 -fabi-version=0 -march=native -mfpmath=sse
我想以从 派生的类的形式使用一些符合 C++ 标准的内存管理std::allocator,但能够分配内存块并以较小的部分释放它们。我只找到了 boost::pool,但这在上述意义上不符合标准。有什么更有用的东西还是我必须自己编写代码?
(请注意,std::allocator对于分配许多小对象,即使用 时,通常是无用的std::list。)
编辑以澄清。
比如说,我想使用std::list许多小对象中的一个,然后std::allocator使用分配每个对象的实现::new会导致运行时的巨大开销(而且我认为还有内存)。分配大块对象并将它们一一分发会更有效。为此,我需要一个std兼容的分配器(不需要从 派生std::allocator,但必须实现相同的概念),它可以与任何库容器一起使用std并提供所需的内存管理,理想情况下允许我告诉它有多少个对象我可能会单独分配。
编辑这不是对静态类成员的未定义引用的重复.那个问题探讨了问题的原因(我在下面解释).在这里,我正在寻找与那些问题的答案中提出的解决方案不同的解决方案(这意味着更改constexpr要使用的变量的声明/定义- 主要是通过在编译单元中添加定义).
我已经创建了一个小的可变参数模板函数make_string()来生成std::string任意数量的io-able参数,如下所示.
using std::ostringstream; // just for this example
inline ostringstream&write(ostringstream&ostr, const char*x)
{ if(x) ostr<<x; return ostr; }
template<class T>
inline ostringstream&write(ostringstream&ostr, T const&x)
{ ostr<<x; return ostr; }
inline ostringstream&write(ostringstream&ostr) noexcept
{ return ostr; }
template<class T, class... R>
inline ostringstream&write(ostringstream&ostr, T const&x, R&&... r)
{ return write(write(ostr,x), std::forward<R>(r)...); }
inline std::string make_string(const char*text)
{ return {text?text:""}; }
inline std::string …Run Code Online (Sandbox Code Playgroud) 我有以下类结构
// file foo.h:
struct foo_base
{ ... }
template<typename T> struct foo : foo_base
{ ... };
template<typename F>
using is_foo = std::is_convertible<F,foo_base>;
template<typename, typename=void> struct aux;
template<typename Foo>
struct aux<Foo, typename std::enable_if<is_foo<Foo>::value>::type>
{ ... }; // specialisation for any foo
Run Code Online (Sandbox Code Playgroud)
// file bar.h:
#include "foo.h"
template<typename T> struct bar : foo<T>
{ ... };
template<typename T>
struct aux<bar<T>>
{ ... }; // specialisation for bar<T>
Run Code Online (Sandbox Code Playgroud)
现在,问题在于提供的aux<bar<T>>两种专业aux都是可行的.有没有办法避免这种歧义而不为每个人提供另一种专业化T?请注意,对文件的修改foo.h不得知道文件bar.h.
注意应解决歧义,以便 …
c++ templates partial-specialization template-specialization c++11
我对gcc 5.1.0有一个奇怪的问题.以下最小代码
// header 1
namespace A {
template<typename X>
inline constexpr X square(X x) { return x*x; }
}
// header 2
namespace A { namespace B {
template<typename X>
struct matrix { X A[3][3]; };
template<typename X>
matrix<X> square(matrix<X> const&) noexcept;
} }
// source, includes both headers
namespace A { namespace B {
using A::square; // no problem without this line
template<typename X>
matrix<X> square(matrix<X> const&M) noexcept
{
matrix<X> R;
for(int i=0; i!=3; ++i)
for(int j=0; j!=3; …Run Code Online (Sandbox Code Playgroud) 如何避免
template <typename Derived>
struct base { int foo() { return static_cast<Derived*>(this)->bar(); } };
struct derived : base<derived> { int bar(); };
struct another_derived : base<derived> { int bar(); }; // error: wrong base
Run Code Online (Sandbox Code Playgroud)
派生类中没有额外的代码?
这个问题之前已经被问过两次 (尽管没有避免在派生类中添加额外代码的额外条件),并给出了推荐的答案
template <typename Derived>
struct base {
int foo() { return static_cast<Derived*>(this)->bar(); }
private:
~base() {}
friend Derived;
};
Run Code Online (Sandbox Code Playgroud)
然而,这不仅可以防止上述错误,而且还可以Base从 访问 的所有私有成员Derived。有没有替代方案可以避免这种情况?或者是否可以最终证明这是不可能的?
编辑
我实际上有一个更复杂的问题,除了通常的用法(如上所述)之外,还有
template<typename Derived>
struct intermediate : base<Derived>
{
int bar() { return static_cast<Derived*>(this)->ark(); …Run Code Online (Sandbox Code Playgroud) 在C++中计算二项式系数的最佳方法是什么?我已经看到了一些代码片段,但在我看来,它总是只在某个特定区域可行.我需要一个非常非常可靠的计算.我尝试使用gamma功能:
unsigned n=N;
unsigned k=2;
number = tgammal(n + 1) / (tgammal(k + 1) * tgammal(n - k + 1));
Run Code Online (Sandbox Code Playgroud)
但它在n = 8时已经不同,k = 2的1(并且n = 30,k = 2它崩溃).我"仅"需要计算大约至少n = 3000,其中k = 2.
c++ ×10
c++11 ×5
templates ×3
allocation ×1
boost ×1
containers ×1
crtp ×1
decltype ×1
gcc ×1
icc ×1
inheritance ×1
math ×1
return-type ×1
return-value ×1