小编Khu*_*hid的帖子

实现C++ 14 make_integer_sequence

我尝试实现C++ 14别名模板make_integer_sequence,这简化了类模板的创建integer_sequence.

template< class T, T... I> struct integer_sequence
{
    typedef T value_type;
    static constexpr size_t size() noexcept { return sizeof...(I) ; }

};

template< class T, T N>
using make_integer_sequence = integer_sequence< T, 0,1,2, ... ,N-1 >; // only for illustration.
Run Code Online (Sandbox Code Playgroud)

为了实现,make_integer_sequence我们需要一个辅助结构make_helper.

template< class T , class N >
using make_integer_sequence = typename make_helper<T,N>::type;
Run Code Online (Sandbox Code Playgroud)

实施make_helper并不太难.

template< class T, T N, T... I >
struct make_helper
{
   typedef …
Run Code Online (Sandbox Code Playgroud)

c++ gcc c++11 c++14

47
推荐指数
3
解决办法
2万
查看次数

c ++ 14中std :: string的运算符后缀

我安装了Clang 3.4,并为字符串文字""s测试后缀运算符.

#include <string>

//1. using namespace std; // without compile error.
//2. using std::operator ""s; // or without it compile error, too.
// for success compiles, need 1. or 2. line.

int main()
{
    auto s = "hello world"s; 
}
Run Code Online (Sandbox Code Playgroud)

如果我评论1和2,我得到编译错误.但我知道在大项目中1.方法很糟糕,2.方法很陌生.

QA:我是否可以使用运营商"" S没有任何文字using namespace stdusing std::operator ""s

c++ c++11 c++14

22
推荐指数
1
解决办法
4398
查看次数

是a = 0; b =(a ++,a + 1); 未定义的行为(UB)?

看简单的例子:

int a = 0;
int b = (a ++  ,  a + 1); // result of b is UB or well defined ?  (c++03).
Run Code Online (Sandbox Code Playgroud)

这在c ++ 11/c ++ 14中有所改变吗?

c++ undefined-behavior c++11

21
推荐指数
1
解决办法
1150
查看次数

为什么unique-ptr不检查基类是否可以虚拟破坏?

考虑这个例子:

#include <cstdio>
#include <memory>

struct base
{
    base( int i ): i(i)    {    printf("base ctor\n"); }
    ~base() {     printf("base non-virtual dtor\n"); } // non-virtual
    int i;
};

struct derived : public base
{
    char* s;
    derived(int i): base(i), s(new char[i] )
    {
        printf("derived ctor\n");
    }
    ~derived()
    {
        printf("derived dtor\n");
        delete [] s;
    }
};

int main()
{
    printf("Success\n");

    //raw pointer
    printf("RAW-POINTER\n");
    {
        base* b = new derived(2);
         // ......
        delete b; //here memory leak, but it's old- and error-prone …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism unique-ptr c++11

15
推荐指数
2
解决办法
3321
查看次数

在constexpr函数体c ++ 14中可以有非文字类型的定义变量吗?

我认为在C++ 14中,从constexpr中删除了更多限制.但根据N3797 7.1.5 3-punct:


contexpr函数shal的定义满足以下约束:

  • 它不应该是虚拟的
  • 其返回类型应为字面类型;
  • 每个参数类型应为文字类型;
  • 它的函数体应为= delete,= default或不包含的复合语句:
    • ASM-定义,
    • 一个goto声明,
    • 一个试块,或
    • 非文字类型或静态或线程存储持续时间的变量的定义,或者不执行初始化的定义.

我知道为什么静态,线程存储持续时间变量是不允许的,但我没有看到任何理由,为什么只允许定义一个文字类型的变量?

或者我不明白标准.

我不确定,但是根据标准的跟随错误应该创建,即使是C++ 14:

struct point{
constexpr point(): x(0), y(0){}
constexpr point(int x_, int y_): x(x_),y(y_){}
constexpr int hypot()const { return x*x + y*y; }
int x,y;
};

constexpr int hypot(int x, int y) {  
   point p{x,y}; //error, because p - is not literal type.
   return p.hypot();
}

// error, because return type …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer constexpr c++11 c++14

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

为什么std :: basic_string的成员在VS2010中是公共的?

#include <iostream>
#include <string>
int main()
{
    std::string s;
    s._Mysize = 7;  // Well compiled !!!
    std::cout << s.size() << '\n'; // prints 7   !!!

}
Run Code Online (Sandbox Code Playgroud)

为什么std::basic_stringVS2010中的非静态成员是公开的?

这个错误吗?如果是的话,下一个版本的visual studio(vs2012和vs2013)怎么样?

编辑:我只测试其他容器,并且...有趣的vector和unique_ptr的非静态成员也是公共的.

std::vector<char> v;
v._Myfirst = (char*)2; // Well Compiled.


std::unique_ptr< int > u;

u._Myptr = 0; // well compiled.
Run Code Online (Sandbox Code Playgroud)

问:使用public数据成员有什么理由或优势吗?

c++ visual-studio-2010 visual-studio-2012

12
推荐指数
1
解决办法
378
查看次数

c ++中的评估顺序初始化数组

我喜欢c ++ 11可变参数模板,所以我经常用它编写一些小代码.

看这个例子:

#include <cstdio>
#include <type_traits>
#include <vector>

template< typename ... T >
auto make_vector(T ... t ) -> std::vector< typename std::common_type<T...>::type >
{
    std::vector< typename  std::common_type<T...>::type > v;
    v.reserve( sizeof...(T) );

    using list = int[];
    (void)list{ 0, ( (void)v.push_back(std::move(t)) ,0)... };
    //                |/ / / /
    //                --------
    //                 \-- How are evaluated v.push_back()s, sequentially or arbitrary ?
    return v;
}

int main()
{
    auto v = make_vector(2, 3.0, 'a', 7UL );

    for(auto e : v ) …
Run Code Online (Sandbox Code Playgroud)

c++ array-initialization variadic-templates c++11

9
推荐指数
1
解决办法
818
查看次数

int i = i ^ i的值; 它总是零或未定义的行为?

在以下程序中,输出始终为零或未定义的行为?

#include<iostream>

int main()
{
    int i= i ^ i ;
    std::cout << "i = " << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

用gcc 4.8.0这段代码编译成功,输出为0.

c++ language-lawyer

4
推荐指数
2
解决办法
497
查看次数

从另一个容器创建容器,在c ++中应用每个元素一些函数

我的问题很简单,参见示例:

std::array<int,6> a = {{0,1,2,3,4,5}}; // -- given container.
auto F = []( int i ) { return  i*i; }; // -- given function.

std::vector<int> v;  // need create

// my solution:
v.reserve( a.size () );
for( std::size_t i = 0; i < a.size(); ++i )
    v.push_back( F(a[i]) ); 



// but I need something like
   std::vector<int>v( a.begin(), a.end(), <|applying each element to F|> );
Run Code Online (Sandbox Code Playgroud)

我可以创建上面的容器,而不是显式调用reserve和任何重新分配吗?

编辑:

  1. 我想避免储备; 因为其他我的第一个解决方案对我有好处:)
  2. 我想避免任何调整大小; 因为默认情况下它会初始化每个元素.
  3. 我将在真实项目中使用它,其中可能包括许多第三方库(boost,Soft-STL,...).

c++ containers iterator stl-algorithm c++11

4
推荐指数
3
解决办法
135
查看次数

为什么这段代码不能用VS2010和gcc 4.8.1编译

很简单,看看这段代码:

namespace B
{

    struct A{ int i; } ;

    A getA(int i);
}

//        ____ if I'll delete '::' then program successfull compiled.
//       /
::B::A  ::B::getA(int i){ ::B::A a = {i}; return a;}

#include <cstdio>
int main()
{

  ::B::A a = ::B::getA(2);

  printf("%d\n", a.i);

}
Run Code Online (Sandbox Code Playgroud)

错误列表VS2010:

1>main.cpp(94): error C3083: 'B': the symbol to the left of a '::' must be a type
1>main.cpp(94): error C2039: 'getA' : is not a member of 'B::A'
1>main.cpp(88) : see declaration of …
Run Code Online (Sandbox Code Playgroud)

c++ gcc visual-studio-2010

3
推荐指数
1
解决办法
93
查看次数

这里有更多的未定义的行为在c ++中,对不起这个问题,但再次UB

有一个非常简单的UB示例:

int i = 1;
i = i++; // classic example of UB.
Run Code Online (Sandbox Code Playgroud)

我最近看到,如何使用Pascal样式inc操作.Eric Niebler github

// this structure little difference than original.
struct inc_t 
{   
  template< typename T> 
  T operator()(T& t ) const { return t++; } 
};

constexpr inc_t inc{};

//usage
int i = 1;
inc(i);
//or
int j = inc(i);
Run Code Online (Sandbox Code Playgroud)

所以,结合:

int i = 1;
i = inc(i); // Is there still UB ?
Run Code Online (Sandbox Code Playgroud)

谢谢.

c++ undefined-behavior language-lawyer c++11

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

用法c ++ 11 std :: tuple在大项目中

C++ 11添加了非常有用的容器std :: tuple,现在我可以将许多结构转换为std :: tuple:

// my Field class
struct Field 
{
     std::string path;
     std::string name;
     int id;
     int parent_id;
     int remote_id;    
};

//create
Field field = {"C:/", "file.txt", 23, 20, 41 };  

 //usage
  foo( field.path );
  field.name= new_name;
  int id = field.id;
Run Code Online (Sandbox Code Playgroud)

//to std::tuple,               /--path,  /--name      /--id, /--parend_id,   /--remote_id
using Field = std::tuple< std::string, std::string , int,   int ,           int >;

//create
auto field = make_tuple<Field>("C:\", "file.txt", 23, 20, 41);      



// usage
     foo( std::get<0>(field) );  // may …
Run Code Online (Sandbox Code Playgroud)

c++ tuples c++11 stdtuple

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