考虑以下typedef:
typedef int (*f1)(float);
typedef f1 (*f2)(double);
typedef f2 (*f3)(int);
Run Code Online (Sandbox Code Playgroud)
f2是一个返回函数指针的函数.与...相同f3,但函数的类型,f3返回的指针是f2.如何在f3没有typedef的情况下定义?我知道typedef是更清晰,更容易理解的定义方式f3.但是,我的目的是更好地理解C语法.
以下代码编译并执行"正确的事情":
#include <boost/variant.hpp>
#include <iostream>
int main()
{
int a = 10;
boost::variant<int&, float&> x = a;
a = 20;
std::cout << boost::get<int&>(x) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
boost :: variant如何存储引用?根据C++标准,如何存储引用完全取决于编译器.实际上,boost::variant甚至知道引用占用了多少字节?sizeof(T&) == sizeof(T),所以它不能使用sizeof()运算符.现在,我知道引用很可能是作为指针实现的,但是语言并不能保证.get<>当变体存储引用时,如何和访问工作的一个很好的解释获得额外的分数:)
在C++中是否有Damas-Hindley-Milner样式推断的实现,最好使用现代C++技术?
我想编写一个C++元函数is_callable<F, Arg>,定义value为true,当且仅当类型F具有表单的函数调用运算符时SomeReturnType operator()(const Arg &).例如,在以下情况中
struct foo {
void operator(const int &) {}
};
Run Code Online (Sandbox Code Playgroud)
我想is_callable<foo, int &>成为false和is_callable<foo, const int &>成为true.这是我到目前为止:
#include <memory>
#include <iostream>
template<typename F, typename Arg>
struct is_callable {
private:
template<typename>
static char (&test(...))[2];
template<unsigned>
struct helper {
typedef void *type;
};
template<typename UVisitor>
static char test(
typename helper<
sizeof(std::declval<UVisitor>()(std::declval<Arg>()), 0)
>::type
);
public:
static const bool value = (sizeof(test<F>(0)) == sizeof(char)); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用c ++ 11功能实现boost :: optional之类的数据结构.这是我到目前为止:
template<typename T>
struct maybe {
bool valid;
union {
T value;
};
maybe() : valid(false) {}
maybe(const T& _v) {
valid = true;
new (&value) T(_v);
}
maybe(const maybe& other) {
if (other.valid) {
valid = true;
new (&value) T(other.value);
}
else valid = false;
}
~maybe() {
if (valid)
value.~T();
}
bool is_valid() { return valid; }
operator T&() {
if (valid) return value;
throw std::bad_exception();
}
};
Run Code Online (Sandbox Code Playgroud)
我利用无限制联合功能为可选值创建一个正确对齐的空间,该空间可以原位存储,而不是动态分配空间.事情主要起作用,除非我想创建一个带引用的<>.例如maybe<int&>导致g ++ 4.7抱怨:
error: …Run Code Online (Sandbox Code Playgroud) 我将车轮文件放在http://long.url.name.com/package-cp27-none-linux_x86_64.whl,然后pip install http://long.url.name.com/package-cp27-none-linux_x86_64.whl,它会很好用。但是,当我有一个URL缩短器时,转发上述URL并得到类似的内容http://short.url/XYZ,然后尝试做pip install http://short.url/XYZ,pip不起作用。我注意到下载活动,但是从pip看到以下错误:
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 18, in <module>
IOError: [Errno 2] No such file or directory: '/tmp/pip-2LPqrK-build/setup.py'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-2LPqrK-build
Run Code Online (Sandbox Code Playgroud)
问题似乎是pip为该文件看到的文件名是XYZ。wget例如,当我尝试使用来下载文件时,情况就是如此。但是,当我进入http://short.url/XYZ该文件时,确实下载并另存为package-cp27-none-linux_x86_64.whl。那么,如何使点子与缩短的URL一起使用呢?
我想存储对象中boost::any对象的引用.如何初始化boost :: any对象?我试过了std::ref(),但是boost::any初始化了std::reference_wrapper<>.例如,以下内容
#include <boost/any.hpp>
#include <cxxabi.h>
#include <iostream>
int main(void)
{
int s;
int i = 0;
boost::any x(std::ref(i));
std::cout << abi::__cxa_demangle(x.type().name(), 0, 0, &s) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
版画
std::reference_wrapper<int>
Run Code Online (Sandbox Code Playgroud)
我想要boost::any包含int&而不是.
这是对这个问题的一种跟进.
#include <iostream>
struct type1 {};
struct type2 {};
void foo(type1 x)
{
std::cout << "foo(type1)" << std::endl;
}
template<typename T>
void bar() {
foo(T());
}
int main()
{
bar<type1>();
bar<type2>();
return 0;
}
void foo(type2 x)
{
std::cout << "foo(type2)" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码foo(type2)是不是在实例化时可见bar<type2>在main.然而代码编译并产生以下输出:
foo(type1)
foo(type2)
Run Code Online (Sandbox Code Playgroud)
如何编译器知道foo(type2)可用实例化时,bar<type2>在main?
编辑:我试图了解更多关于模板实例化过程中的重载决策是如何工作的.请考虑以下代码:
#include <iostream>
struct type1 {};
struct type2 {};
struct type3 {
operator type2() { return type2(); } …Run Code Online (Sandbox Code Playgroud) 我编写了以下代码,模板参数推导失败:
template<int>
struct num {};
template<int m>
void match(num<2*m>) {
}
int main()
{
match(num<2>());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我从直觉中知道编译器不能推断出正确的m,但我想理解为什么它失败的理论基础.有人可以澄清吗?
以下代码无法编译.错误消息是:
错误1:
error C3930: 'foo' : no overloaded function has restriction specifiers that are compatible with the ambient context ''
Run Code Online (Sandbox Code Playgroud)
错误2:
error C2660: 'f1' : function does not take 0 arguments
Run Code Online (Sandbox Code Playgroud)
错误3:
IntelliSense: amp-restricted function "int foo() restrict(amp)" (declared at line 5) must be called from an amp-restricted function
Run Code Online (Sandbox Code Playgroud)
该程序:
#include <amp.h>
#include <iostream>
using namespace std;
int foo() restrict(amp) { return 5; }
int f1(int x = foo()) restrict(amp) {
return x;
}
int main()
{
using namespace concurrency; …Run Code Online (Sandbox Code Playgroud) 使用一些Hindley-Milner类型推断变体的编程语言可以很容易地推断出表达式的类型,例如
let rec fix f x = f (fix f) x
Run Code Online (Sandbox Code Playgroud)
而C++ 1y中的返回类型推断因以下原因而失败:
int main() {
auto fix =
[&](auto f) {
return [&](auto x) {
return f(fix(f))(x);
};
};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用clang 3.5和命令试了这个
clang++ -std=c++1y fix.cc
Run Code Online (Sandbox Code Playgroud)
我明白了
fix.cc:7:18: error: variable 'fix' declared with 'auto' type cannot appear in its
own initializer
return f(fix(f))(x);
Run Code Online (Sandbox Code Playgroud)
C++的返回类型推断中缺少什么,当必须推断返回类型时,它不允许在它自己的初始化器中使用变量?我可以做些什么来解决这个问题,更好的是,我们可以做些什么来解决这个问题?
我想以多线程方式实现分支定界搜索.特别是,我想用来async包装每个分支的搜索调用,然后等到某个线程得到答案,然后退出.(理想情况下,我想取消其他线程,但线程取消不在标准中).这是一些简化的代码:
#include <iostream>
#include <random>
#include <future>
#include <thread>
using namespace std;
mt19937 rng;
uniform_int_distribution<unsigned> random_binary(0, 1);
bool search() {
return static_cast<bool>(random_binary(rng));
}
#define N 10000
int main()
{
rng.seed(42);
std::vector<future<bool>> tasks;
for (unsigned i=0; i<N; ++i)
tasks.push_back(async(launch::async, search));
// Don't want to wait sequentially here.
for (unsigned i=0; i<N; ++i) {
tasks[i].wait();
if (tasks[i].get()) {
cout << "i = " << i << "\n";
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
search()是搜索功能.它根据是否找到答案返回true/false.我回答一个随机的答案来说明.但问题的关键在于调用的for循环tasks[i].wait().现在,我正在等待任务完成.相反,我想做这样的事情:
auto x = …Run Code Online (Sandbox Code Playgroud)