我尝试实现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) 我安装了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 std和using std::operator ""s?
看简单的例子:
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中有所改变吗?
考虑这个例子:
#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++ 14中,从constexpr中删除了更多限制.但根据N3797 7.1.5 3-punct:
contexpr函数shal的定义满足以下约束:
我知道为什么静态,线程存储持续时间变量是不允许的,但我没有看到任何理由,为什么只允许定义一个文字类型的变量?
或者我不明白标准.
我不确定,但是根据标准的跟随错误应该创建,即使是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) #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 ++ 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) 在以下程序中,输出始终为零或未定义的行为?
#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.
我的问题很简单,参见示例:
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和任何重新分配吗?
编辑:
很简单,看看这段代码:
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) 有一个非常简单的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++ 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++ ×12
c++11 ×9
c++14 ×3
gcc ×2
constexpr ×1
containers ×1
iterator ×1
polymorphism ×1
stdtuple ×1
tuples ×1
unique-ptr ×1