我(想想我)遵循Facebook教程,但我无法正常工作.我正在尝试通过FB登录,然后获取有关用户的信息.我有:
private Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
System.out.println("GOOD");
if (session.isOpened()) {
System.out.println("AWESOME");
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
m_user = user;
System.out.println("Hello " + user.getName());
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
public void onBtnFacebookClick(final View v) {
Session session = Session.getActiveSession();
if (session == null) {
session = new Session(this);
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new …
Run Code Online (Sandbox Code Playgroud) 假设我有:
struct A
{
A(int x) : m_x(x) { }
A(A&&) = delete;
int m_x;
}
Run Code Online (Sandbox Code Playgroud)
和:
A a(3); // Ok
auto a = A(3); // Error: function A(int&&) cannot be referenced - it's a deleted function
Run Code Online (Sandbox Code Playgroud)
为什么后者调用移动构造函数?为什么这两个语句在生成代码方面有所不同?
我有两个结构:
struct MyVector {
storage: Vec<u32>,
}
struct MyVectorBuilder {
storage: Vec<u32>,
}
impl MyVectorBuilder {
fn new() -> MyVectorBuilder {
MyVectorBuilder { storage: Vec::new() }
}
fn build_my_vector(&mut self) -> MyVector {
// Doesn't compile: ^^^^ cannot move out of borrowed content
MyVector { storage: self.storage }
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉编译器MyVectorBuilder
在调用后不会使用build_my_vector()
它所以它会让我移动storage
到MyVector
?
我正在尝试将 C# 方法传递给 Rust 以用作回调。我已经成功地传递了一个静态函数并且它工作正常(见下文)。
现在我想调用一个实例方法,这意味着trigger
下面的函数也应该接收一个不透明的 ( libc::c_void
) this 指针。我怎样才能得到哪个IntPtr
Rust应该作为实例指针传递给回调函数?
C#
class Program
{
delegate void PrintFn(int x);
public static void Main(string[] args)
{
PrintFn fn = Print;
var ptr = Marshal.GetFunctionPointerForDelegate(fn);
IntPtr handle = IntPtr.Zero;
create(ptr, out handle);
trigger(handle, 3);
}
public static void Print(int x)
{
Console.WriteLine($"C#: {x}");
}
[DllImport(@"/path/to/rust/dll")]
public static extern int create(IntPtr readFn, out IntPtr callback);
[DllImport(@"/path/to/rust/dll")]
public static extern int trigger(IntPtr handle, int x);
}
Run Code Online (Sandbox Code Playgroud)
锈:
use std::boxed::Box;
pub …
Run Code Online (Sandbox Code Playgroud) 我想做一个typedef
取决于typedef
模板参数中是否存在的:
struct foo
{
using MyType = int;
};
template <typename T = foo>
struct bar
{
// Pseudo code
#if T::MyType is defined
using MyType = T::MyType;
#else
using MyType = double;
#endif
};
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以使其std::conditional
在C ++ 14中使用?
我一直在阅读维基百科的Astar 文章.在他们的实现中,他们检查每个节点是否在closed
集合中,如果是,他们跳过它.难道就没有可能,如果启发式是容许的,但不是一致的,我们可能需要以提高它的两次(或更多)重温节点f
值?这是相关的代码
for each neighbor in neighbor_nodes(current)
if neighbor in closedset //This if condition bothers me
continue
tentative_g_score := g_score[current] + dist_between(current,neighbor)
if neighbor not in openset or tentative_g_score < g_score[neighbor]
came_from[neighbor] := current
g_score[neighbor] := tentative_g_score
f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
if neighbor not in openset
add neighbor to openset
Run Code Online (Sandbox Code Playgroud) 运行时:
template <typename T>
struct CodeByType
{
static const int32_t Value = 7;
};
template <>
struct CodeByType<int>
{
static const int32_t Value = 1;
};
template <typename Arg, typename... Args>
int32_t Sum()
{
// The compiler complains on this line
return Sum<Arg>() + Sum<Args...>();
}
template <typename Arg>
int32_t Sum()
{
return CodeByType<Arg>::Value;
}
int main()
{
auto sum = Sum<int, char, double>();
}
Run Code Online (Sandbox Code Playgroud)
我越来越:
错误C2668'Sum':对重载函数的模糊调用
有人可以解释为什么以及如何克服它?
这看起来非常类似于下面的代码,它编译,所以我想它与Sum
不接受任何实际参数有关.
template <typename T>
T adder(T first) {
return first; …
Run Code Online (Sandbox Code Playgroud) 我有一个正在尝试遍历的错误链接列表。给定MyError
,它是带有可选代码的错误链接列表,我的目标是遍历该链并返回第一个非None
代码:
type BoxedError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[derive(Debug)]
struct MyError {
code: Option<u32>,
source: Option<BoxedError>,
}
impl std::error::Error for MyError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source().map(|s| s as _)
}
}
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt("", f)
}
}
impl MyError {
fn source(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
self.source.as_ref().map(|c| c.as_ref())
}
fn …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个std::sort
模板比较类,该类应该接收未知数量的元组(可变模板)。每个元组都应由一列(我们的代码中有某种类型)和布尔值组成,并指定该列应按升序还是降序排序。
基本上,我想要类似的东西:
// doesn't compile - conceptual code
template <typename std::tuple<Col, bool>>
struct Comparator
{
bool operator() (int lhs, int rhs)
{
// lhs and rhs are row indices. Depending on the columns
// and the bools received, decide which index should come first
}
}
Run Code Online (Sandbox Code Playgroud)
在C ++ 11中这种事情可能吗?
我偶然发现其实现Optional<T>
基于LLVM的Optional.h类,并且无法弄清楚为什么它的实现方式如此.
为了简短起见,我只是粘贴了我不理解的部分:
template <typename T>
class Optional
{
private:
inline void* getstg() const { return const_cast<void*>(reinterpret_cast<const void*>(&_stg)); }
typedef typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type storage_type;
storage_type _stg;
bool _hasValue;
public:
Optional(const T &y) : _hasValue(true)
{
new (getstg()) T(y);
}
T* Get() { return reinterpret_cast<T*>(getstg()); }
}
Run Code Online (Sandbox Code Playgroud)
我能想到的最天真的实现:
template <typename T>
class NaiveOptional
{
private:
T* _value;
bool _hasValue;
public:
NaiveOptional(const T &y) : _hasValue(true), _value(new T(y))
{
}
T* Get() { return _value; }
}
Run Code Online (Sandbox Code Playgroud)
问题: