支持右值构造。在写复合语句--嗯,就是Meyers在《effective stl》里面说的那种write only的代码时候--常常发生这种情况。现在好了,是不是我可以写更多的write only的代码了?并且,考虑把所有的T& 替换成T&&?^_^
Rvalue constructors
• Often we copy a value just before it is
destroyed
● Example
template<class T> swap(T& a, T& b)
{
T t = a; // now we have two copies of a
a = b; // now we have two copies of b (and one of a)
b = t; // now we have two copies of t (and one of b)
}
Swap(v,vector<int>()); // error: lvalue required
Rvalue constructors
• Such “shuffling around” of values is very
common in library code
● Example
template<class T> swap(T&& a, T&& b)
{
T&& t = a;
a = b;
b = t;
}
Swap(v,vector<int>()); // ok: v becomes the empty vector
初始化的福音来了,只是这个std::Seq_init<T>没有看明白到底怎么实现它。
template<class T> class vector {
// …
vector(std::Seq_init<T>); // sequence constructor
// …
};
vector<double> v = { 1, 2, 3.4 };
vector<string> geek_heros = {
“Dahl”, “Kernighan”, “McIlroy”,
“Nygaard”, “Richie”, “Stepanov” };
这里,如果f的原型是void f(int, std::vector<int>, int);是不是一样可行?那就太帅了!
C++0x: initializer lists
• Not just for templates and constructors
void f(int, std::Seq_init<int>, int);
f(1, {2,3,4}, 5};
到底还有些什么呢?期待!
• “More advanced” sequences should be in
the standards library – this is just a (code
language) way of handling initializer lists
终于,看到闪亮的auto登场了!再也不要写冗长的vector<Myclass>::iterator itr了,aoto itr就可以。
This’ll work
vector<vector<double>> tbl; // 这个语言的Bug也算是大名鼎鼎,终于寿终正寝了。
auto p = find(tbl.begin(), tbl.end(), x);
// p becomes vector<vector<double>>::const_iterator
• The >> and auto have already been approved for C++0x
• “Supporting novices of all backgrounds” requires work on
both the language and the standard library.
• Concerns for education will be central for that
● E.g., “Learning Standard C++ as a new Language” [Stroustrup,
1999].
这种简化,早就该来了,惠而不费的东西。
• Overloading based on concepts, enables further
simplification
auto p = find(tbl, x);
我看到下面这些东西的时候,真的很吃惊,真的!“a type system for C++ types”,这意味着什么?
元数据、反射、aop,会一个接一个到来吗?至少,会一部分到来的,我相信!
Concepts
(still in the design stage)
• “a type system for C++ types”
• Based on
● Analysis of design alternatives
• 2003 papers (Stroustrup & Dos Reis)
● Design by Stroustrup & Dos Reis (“Texas proposal”)
• April 2005 paper, September 2005 papers
● Design by Siek, et al (“Indiana proposal”)
• February 2005 paper, September 2005 papers
• These proposals are being merged into one
终于,C#中的Concept check也来了。只是,C#的check基于接口,在内部数据类型上的check失败,让我很
是受打击。
Concepts: checking
• The checking of use happens at the call site
and uses only the declaration
template<Forward_iterator For, Value_type V>
where Assignable<For::value_type,V>
void fill(For first, For last, const V& v); // just a declaration, not definition
int i = 0;
int j = 9;
fill(i, j, 9.9); // error: int is not a Forward_iterator
int* p= &v[0];
int* q = &v[9];
fill(p, q, 9.9); // ok: int* is a Forward_iterator
呵呵,如此简练的语法。。。effil的contract是不是有点类似?
我能不能把contract写进concept check?或者,我能不能写一个contract concept?
即使我只能实现pre_condition,也是一大进步!还有,这个concept check是动态的,还是静态检查的?
看的不是很明白。
template<Randon_access_iterator Iter>
void sort(Iter first, Iter last);
template<Randon_access_iterator Iter, Compare Comp>
where Assignable<Comp::argument_type, Iter::value_type>
&& Callable<Comp, Iter::value_type>
void sort(Iter first, Iter last, Comp comp);
template<Container Cont>
void sort(Cont& c);
template<Container Cont, Compare Comp>
where Assignable<Comp::argument_type, Cont::value_type>
&& Callable<Comp, Iter::value_type>
void sort(Cont& c, Comp comp);
concept可以重载决议。呵呵谁说gp和oo不相容?gp也是个很爱学习的孩子。
Overloading on concepts
void f(vector<int>& vi, Fct f)
{
sort(vi);
sort(vi, f); // call container version
sort(vi.begin(), vi.end());
sort(vi.begin(), vi.end(), f); // call random access iterator version
}
• Currently, this requires a mess of helper
functions and traits
● For this example, some of the traits must be explicit (user visible)
我看了这个以后最大的疑问是:concept check的过程是动态的,还是静态的?运行期会进行检查吗?
检查的话,效率怎么办?不检查,那下面代码里的赋值怎么完成?现在的concept check库我只见过
静态检查的。如果concept check是动态检查,而且又能兼顾效率的话,支持一下contract吧。
Defining concepts
concept Forward_iterator<class Iter>{
Var<Iter>p; // uninitialized
Iter q =p; // copy initialization
p = q; // assignment
Iter& q = ++p; // can pre-increment, result usable as an Iter&
const Iter& cq = p++; // can post-increment, result convertible to Iter
bool(p==q); // equality comparisons, result convertible to bool
bool(p!=q);
typename Iter::value_type; // Iter has an associated type “value_type”
Iter::value_type v = *p; // *p can initialize Iter’s value type
*p = v; // Iter’s value type can be assigned to *p
};
呵呵,不变式的身影也是无处不在啊。对于这个,双手支持!不仅要静态类型安全,也要静态
concept安全。看到这里,觉得concept动态检查的可能性大增。不知道是好还是不好。
Explicit concept asserts
• we can say “unless X is a Forward_iterator the
compilation should fail”
model_assert Forward_iterator<X>;
• The exact details are under vigorous debate
● “Model asserts” are necessary but their use must be
optional
Explicit concept asserts
// Is int* a forward iterator?
// of course!
// But we just said that every forward iterator had a member type “value_type”?
// Let’s give it one:
// when we use an int* as a Forward_iterator,
// value_type should be considered a member of T* with the “value” int:
model_assert template<Value_type T> Forward_iterator<T*> {
using T*::value_type = T;
};
模版的部分typedef.真的是期待以久了,最好在标准出台以前,主流编译器就已经支持了。
template<class T> using Vec = vector<T,My_alloc<T>>; //模版部分的typedef
Vec<double> v = { 2.3, 1.2, 6.7, 4.5 };
sort(v);
for(auto p = v.begin(); p!=v.end(); ++p) cout << *p << endl;
Rvalue constructors
• Often we copy a value just before it is
destroyed
● Example
template<class T> swap(T& a, T& b)
{
T t = a; // now we have two copies of a
a = b; // now we have two copies of b (and one of a)
b = t; // now we have two copies of t (and one of b)
}
Swap(v,vector<int>()); // error: lvalue required
Rvalue constructors
• Such “shuffling around” of values is very
common in library code
● Example
template<class T> swap(T&& a, T&& b)
{
T&& t = a;
a = b;
b = t;
}
Swap(v,vector<int>()); // ok: v becomes the empty vector
初始化的福音来了,只是这个std::Seq_init<T>没有看明白到底怎么实现它。
template<class T> class vector {
// …
vector(std::Seq_init<T>); // sequence constructor
// …
};
vector<double> v = { 1, 2, 3.4 };
vector<string> geek_heros = {
“Dahl”, “Kernighan”, “McIlroy”,
“Nygaard”, “Richie”, “Stepanov” };
这里,如果f的原型是void f(int, std::vector<int>, int);是不是一样可行?那就太帅了!
C++0x: initializer lists
• Not just for templates and constructors
void f(int, std::Seq_init<int>, int);
f(1, {2,3,4}, 5};
到底还有些什么呢?期待!
• “More advanced” sequences should be in
the standards library – this is just a (code
language) way of handling initializer lists
终于,看到闪亮的auto登场了!再也不要写冗长的vector<Myclass>::iterator itr了,aoto itr就可以。
This’ll work
vector<vector<double>> tbl; // 这个语言的Bug也算是大名鼎鼎,终于寿终正寝了。
auto p = find(tbl.begin(), tbl.end(), x);
// p becomes vector<vector<double>>::const_iterator
• The >> and auto have already been approved for C++0x
• “Supporting novices of all backgrounds” requires work on
both the language and the standard library.
• Concerns for education will be central for that
● E.g., “Learning Standard C++ as a new Language” [Stroustrup,
1999].
这种简化,早就该来了,惠而不费的东西。
• Overloading based on concepts, enables further
simplification
auto p = find(tbl, x);
我看到下面这些东西的时候,真的很吃惊,真的!“a type system for C++ types”,这意味着什么?
元数据、反射、aop,会一个接一个到来吗?至少,会一部分到来的,我相信!
Concepts
(still in the design stage)
• “a type system for C++ types”
• Based on
● Analysis of design alternatives
• 2003 papers (Stroustrup & Dos Reis)
● Design by Stroustrup & Dos Reis (“Texas proposal”)
• April 2005 paper, September 2005 papers
● Design by Siek, et al (“Indiana proposal”)
• February 2005 paper, September 2005 papers
• These proposals are being merged into one
终于,C#中的Concept check也来了。只是,C#的check基于接口,在内部数据类型上的check失败,让我很
是受打击。
Concepts: checking
• The checking of use happens at the call site
and uses only the declaration
template<Forward_iterator For, Value_type V>
where Assignable<For::value_type,V>
void fill(For first, For last, const V& v); // just a declaration, not definition
int i = 0;
int j = 9;
fill(i, j, 9.9); // error: int is not a Forward_iterator
int* p= &v[0];
int* q = &v[9];
fill(p, q, 9.9); // ok: int* is a Forward_iterator
呵呵,如此简练的语法。。。effil的contract是不是有点类似?
我能不能把contract写进concept check?或者,我能不能写一个contract concept?
即使我只能实现pre_condition,也是一大进步!还有,这个concept check是动态的,还是静态检查的?
看的不是很明白。
template<Randon_access_iterator Iter>
void sort(Iter first, Iter last);
template<Randon_access_iterator Iter, Compare Comp>
where Assignable<Comp::argument_type, Iter::value_type>
&& Callable<Comp, Iter::value_type>
void sort(Iter first, Iter last, Comp comp);
template<Container Cont>
void sort(Cont& c);
template<Container Cont, Compare Comp>
where Assignable<Comp::argument_type, Cont::value_type>
&& Callable<Comp, Iter::value_type>
void sort(Cont& c, Comp comp);
concept可以重载决议。呵呵谁说gp和oo不相容?gp也是个很爱学习的孩子。
Overloading on concepts
void f(vector<int>& vi, Fct f)
{
sort(vi);
sort(vi, f); // call container version
sort(vi.begin(), vi.end());
sort(vi.begin(), vi.end(), f); // call random access iterator version
}
• Currently, this requires a mess of helper
functions and traits
● For this example, some of the traits must be explicit (user visible)
我看了这个以后最大的疑问是:concept check的过程是动态的,还是静态的?运行期会进行检查吗?
检查的话,效率怎么办?不检查,那下面代码里的赋值怎么完成?现在的concept check库我只见过
静态检查的。如果concept check是动态检查,而且又能兼顾效率的话,支持一下contract吧。
Defining concepts
concept Forward_iterator<class Iter>{
Var<Iter>p; // uninitialized
Iter q =p; // copy initialization
p = q; // assignment
Iter& q = ++p; // can pre-increment, result usable as an Iter&
const Iter& cq = p++; // can post-increment, result convertible to Iter
bool(p==q); // equality comparisons, result convertible to bool
bool(p!=q);
typename Iter::value_type; // Iter has an associated type “value_type”
Iter::value_type v = *p; // *p can initialize Iter’s value type
*p = v; // Iter’s value type can be assigned to *p
};
呵呵,不变式的身影也是无处不在啊。对于这个,双手支持!不仅要静态类型安全,也要静态
concept安全。看到这里,觉得concept动态检查的可能性大增。不知道是好还是不好。
Explicit concept asserts
• we can say “unless X is a Forward_iterator the
compilation should fail”
model_assert Forward_iterator<X>;
• The exact details are under vigorous debate
● “Model asserts” are necessary but their use must be
optional
Explicit concept asserts
// Is int* a forward iterator?
// of course!
// But we just said that every forward iterator had a member type “value_type”?
// Let’s give it one:
// when we use an int* as a Forward_iterator,
// value_type should be considered a member of T* with the “value” int:
model_assert template<Value_type T> Forward_iterator<T*> {
using T*::value_type = T;
};
模版的部分typedef.真的是期待以久了,最好在标准出台以前,主流编译器就已经支持了。
template<class T> using Vec = vector<T,My_alloc<T>>; //模版部分的typedef
Vec<double> v = { 2.3, 1.2, 6.7, 4.5 };
sort(v);
for(auto p = v.begin(); p!=v.end(); ++p) cout << *p << endl;