给定一个数组a,我想countof(a)将数组中的元素数作为编译时常量.如果我有一个指针p,我想countof(p)不编译.这似乎应该是(1)直截了当,(2)通常涵盖在SO中,但(1)我无法使它工作,并且(2)搜索SO没有发现任何东西.
这是我的尝试.
#include <cstddef>
#include <type_traits>
template<typename T, std::size_t n,
typename = typename std::enable_if<std::is_array<T>::value>::type>
constexpr std::size_t countof(T (&)[n]) { return n; }
template<typename T,
typename = typename std::enable_if<std::is_pointer<T>::value>::type>
void countof(T*) = delete;
int main()
{
int a[10];
auto asize = countof(a); // should compile
static_assert(countof(a) == 10,
"countof(a) != 10!");
int *p;
auto psize = countof(p); // shouldn't compile
}
Run Code Online (Sandbox Code Playgroud)
救命?
C++允许通过创建一个operator T()where T我们想要转换为的类型来重载类型转换.
现在,此功能如何与引用一起使用?例如:
struct Y{ int i; };
struct X{
Y y;
operator Y() const { return y; }
};
Run Code Online (Sandbox Code Playgroud)
在这里,我们可以强制X转换Y包含的内容Y.但是,如果我们想要对某个Y参考进行强制转换呢?例如,C++允许我们这样做:
struct X{
Y y;
operator Y&(){ return y; }
operator const Y&() const { return y; }
};
Run Code Online (Sandbox Code Playgroud)
现在,我们可以X转换为Y引用或const引用(也适用于a const X).
第一个和第二个例子的语义有什么不同吗?如果我想允许转换为引用,最好的方法是什么?
我可以想象,即使我operator T()没有任何写入&,C++可能允许转换通过引用返回结果(即,operator T&()当我指定时它可能以某种方式添加隐式方法operator T()).
例如,我想要以下代码工作:
int main(){
X x;
Y& …Run Code Online (Sandbox Code Playgroud) 在c ++ 11中,我有一个非常整洁和工作的代码,用于std::tuple逐个类型地提取项目(我知道这个功能甚至可以放到 c ++ 14 stl)
现在我面临着通过继承的类规范选择项目的任务
struct A
{
int a;
};
struct B : public A
{
int b;
};
...
auto tval = std::make_tuple(1, B());
//now I would like to reference items as following:
tuple_ref_by_inheritance<A>(tval).a = 5; //Access to B instance by parent A
Run Code Online (Sandbox Code Playgroud)
以下代码是我不成功的尝试:
template< class T, class Tuple >
struct tuple_ref_index;
// recursive case
template<class T, class Head, class... Tail >
struct tuple_ref_index<T, std::tuple<Head, Tail...> >
{
enum { …Run Code Online (Sandbox Code Playgroud) 有一种技术我有时会在覆盖这样的template函数时使用:
#include <utility>
template<int> struct unique_enum { enum class type {}; };
template<int index> using UniqueEnum = typename unique_enum<index>::type;
template<bool b, int index=1>
using EnableFuncIf = typename std::enable_if< b, UniqueEnum<index> >::type;
template<bool b, int index=1>
using DisableFuncIf = EnableFuncIf<!b, -index>;
// boring traits class:
template<typename T>
struct is_int : std::false_type {};
template<>
struct is_int<int> : std::true_type {};
#include <iostream>
// use empty variardic packs to give these two SFINAE functions different signatures:
template<typename C, EnableFuncIf< is_int<C>::value >...>
void …Run Code Online (Sandbox Code Playgroud) 考虑以下C++ 11代码:
struct C {};
void f(int(C));
Run Code Online (Sandbox Code Playgroud)
类型是否f相同:
typedef int T(C);
void f(T);
Run Code Online (Sandbox Code Playgroud)
或者是这样的:
void f(int C);
Run Code Online (Sandbox Code Playgroud)
也就是说,应该将(C)其解释为declarator参数名称的一个C,还是作为abstract-declarator函数参数的一个?
标准中指定的是哪里?
即:
[](auto const& foo) {
??? bar; // should be same base type as foo, minus const&
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我正在使用:
typename std::remove_const<typename std::remove_reference<decltype(foo)>::type>::type combination
Run Code Online (Sandbox Code Playgroud)
但我真的希望这是一个更容易的选择!
在范围退出上运行lambda似乎是一个基本的东西,我希望它是标准化的.unique_ptr当它们适用时,情况会更好,但我发现需要无穷无尽的"一次性"析构函数,特别是在利用C风格的库时.有谁知道这是否会到来?
早在2010年,Herb Sutter 就在Dobb博士的一篇文章中提倡使用活性物体而不是裸线.
这是一个C++ 11版本:
class Active {
public:
typedef std::function<void()> Message;
Active(const Active&) = delete;
void operator=(const Active&) = delete;
Active() : done(false) {
thd = std::unique_ptr<std::thread>(new std::thread( [=]{ this->run(); } ) );
}
~Active() {
send( [&]{ done = true; } );
thd->join();
}
void send(Message m) { mq.push_back(m); }
private:
bool done;
message_queue<Message> mq; // a thread-safe concurrent queue
std::unique_ptr<std::thread> thd;
void run() {
while (!done) {
Message msg = mq.pop_front();
msg(); // execute message …Run Code Online (Sandbox Code Playgroud) C++ 11有6种不同的正则表达式语法可供使用.在我的例子中,我正在与使用修改的ECMAScript正则表达式的组件进行交互.
我需要创建一个正则表达式"匹配以X开头的字符串",其中X是我的字符串文字.
所以我想要的正则表达式是粗略的^X.*.除了字符串X可以包含更多的正则表达式特殊字符,我希望它们发生.
这意味着我真的想要^ escaped(X) .*.
现在,我可以阅读ECMAScript文档,找到具有特殊含义的所有字符,编写一个逃避它们的函数,然后完成.但这似乎不优雅,低效且容易出错 - 特别是如果我想支持当前C++支持的所有6种正则表达式,更不用说将来了.
标准中是否有一种简单的方法可以将文字字符串转换为嵌入C++正则表达式,可能是正则表达式语法的函数,还是我必须自己滚动?
下面是使用boost库的类似问题,其中转义列表是硬编码的,然后生成一个反斜杠的正则表达式.我是否已经缩减为适应该答案std?
所以,operator[]不直接说s[s.size()]必须是s[s.size()-1]内存之后的人物.似乎措辞是为了避免提出这种说法.
但是s.data()说s.data()+k == &s[k],并且s.data()必须返回一个指针.
忽略&在CharT上面使用而不是上面使用的看似标准缺陷,std::addressof实现CharT是否s[s.size()]可以在第一次调用之前自由地返回不同的(例如,在受保护的页面上,或在ROM中)s.data()?(显然,它可以将缓冲区安排在一个只读的页面上结束;我说的是另一种情况)
要明确:
据我所知,如果s.data()永远不会被调用(并且编译器可以证明它),那么s[s.size()]就不需要与缓冲区的其余部分连续.
可以在调用之后进行std::addressof(s[s.size()]) 更改,s.data()并且实现符合标准(只要之前s.data()+k == &s[k]已经过.data()评估[],但编译器可以自由执行).或者是否有我看不到的不变性要求?