小编eye*_*ash的帖子

为什么C++中不存在引用成员?

在C++中,我可以在函数指针和函数引用之间进行选择(或者为了完整性,甚至是函数值):

void call_function_pointer (void (*function)()) {
    (*function) ();
}
void call_function_reference (void (&function)()) {
    function ();
}
void call_function_value (void function()) {
    function ();
}
Run Code Online (Sandbox Code Playgroud)

然而,当谈到方法时,我似乎没有在指针和引用之间做出这种选择.

template <class T> void call_method_pointer (T* object, void (T::*method)()) {
    (object->*method) ();
}
// the following code creates a compile error
template <class T> void call_method_reference (T& object, void (T::&method)()) {
    object.method ();
}
Run Code Online (Sandbox Code Playgroud)

这使我假设C++中不存在方法引用.真的吗?如果是,他们不存在的原因是什么?

c++ pointers reference pointer-to-member

35
推荐指数
1
解决办法
3003
查看次数

如何向CMake添加框架

我写了一个需要Cocoa框架的小helloworld程序.我想知道如何在CMake中添加框架.我发现的所有信息都已过时.我在Snow Leopard上有CMake 2.8.1.

macos frameworks cmake

12
推荐指数
2
解决办法
1万
查看次数

constexprinitializer_list 使用 MSVC 不产生输出

以下程序在 GCC 和 Clang 中编译时不会发出警告,并产生预期的输出:

#include <initializer_list>
#include <iostream>

constexpr std::initializer_list<std::initializer_list<const char*>> list = {
    {"a", "b", "c"},
    {"d"}
};

int main() {
    for (const auto& outer: list) {
        std::cout << "level:\n";
        for (const auto& inner: outer) {
            std::cout << "  " << inner << "\n";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,使用 MSVC,该程序根本不会产生任何输出。

根据 C++ 标准,该程序有效吗?这是 MSVC 中的错误吗?如果这不是有效的 C++ 那么为什么 GCC 或 Clang 没有发出警告?有没有更好的方法来创建constexpr内部列表没有固定大小的嵌套列表?

c++ language-lawyer stdinitializerlist

11
推荐指数
1
解决办法
215
查看次数

如何在C++中传递参数包?

请考虑以下示例:

template <class T> class method_traits;
template <class T, class Ret, class... Arg> class method_traits<Ret(T::*)(Arg...)> {
public:
    using type = Arg; // this does not work
};

template <class T> using argument_types = typename method_traits<T>::type;

template <class T> class Node {
    T t;
public:
    Node(Input<argument_types<decltype(&T::process)>>... inputs) { // how do I make this work?
        ...
    }
};
Run Code Online (Sandbox Code Playgroud)

构造函数Node<T>的参数取决于方法的参数T::process.因此,如果类型T具有process签名方法,float process(float a, int b)则构造函数的签名Node<T>应如下所示:Node(Input<float> a, Input<int> b).

如何从 …

c++ templates template-meta-programming variadic-templates c++11

10
推荐指数
1
解决办法
1030
查看次数

Variadic模板代码在GCC 4.6上编译,但不在clang或GCC 4.7上编译

我有这个代码(从更复杂的版本简化):

template <class... A1> class Test {
    public:
    template <class... A2> void print (void(*function)(A2...,A1...)) {

    }
};

void test_print (int a, float b, double c) {

}

int main () {
    Test<float,double> test;
    test.print<int> (&test_print);
}
Run Code Online (Sandbox Code Playgroud)

如果我在GCC 4.6.3上编译它使用g++ -std=c++0x filename.cpp它编译就好了但是在clang 3.0上使用clang++ -std=c++0x filename.cpp它会抛出以下错误:

filename.cpp:14:10: error: no matching member function for call to 'print'
    test.print<int> (&test_print);
    ~~~~~^~~~~~~~~~
filename.cpp:3:33: note: candidate template ignored: failed template argument deduction
    template <class... A2> void print (void(*function)(A2...,A1...)) {
                                ^
1 error generated. …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11

9
推荐指数
1
解决办法
608
查看次数

如何使用CMake链接Windows系统库?

我正在构建一个简单的Windows应用程序,需要链接到gdiplus和winmm.
以前我用过

find_library(GDIPLUS gdiplus)
target_link_libraries(target ${GDIPLUS})
Run Code Online (Sandbox Code Playgroud)

但是当Visual Studio 2015目标与Visual C++ Build Tools一起使用时,CMake找不到该库.根据这里的答案,我应该放弃find_library并且只是使用

target_link_libraries(target gdiplus.lib)
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否只适用于Visual Studio,或者也适用于Cygwin.
那么从CMake链接Windows系统库的正确(或最佳)方法是什么?

windows dll linker cmake

6
推荐指数
1
解决办法
480
查看次数

Typescript 如何找到 .d.ts 文件?

我是一名新的打字稿开发人员,我正在开发一个使用打字稿和 webpack 的项目,一些用 js 编码的库需要 .d.ts 文件,但我很困惑 webpack 如何找到带有 ? 的.d.ts文件import * as PIXI from 'pixi.js'​​?

这是我的猜测:

  1. webpack会找到“node_modules”命名的文件夹pixi.js,并在其中找到 package.json,如果有该types属性,则选择该文件作为.d.ts文件。

  2. 如果按照1的方式找不到.d.ts文件,则进入node_modules/@types(或其他自定义文件夹),查找是否存在同名文件夹pixi.js,并选择index.d.ts文件为文件.d.ts。

  3. 顺便说一句,我在index.d.ts中发现他们使用了module,为什么?我知道官方不推荐这个module关键字(用 代替namespace)。

typescript webpack

6
推荐指数
1
解决办法
4503
查看次数

精益4是懒惰还是严格?

Lean 4 是一种纯粹的函数式编程语言,但它是惰性的(如 Haskell)还是严格的(如 Idris)?这意味着什么?有没有办法选择加入(或选择退出)懒惰?

lazy-evaluation lean

6
推荐指数
1
解决办法
539
查看次数

Lean中有前缀表示法吗?

在 Haskell 中,我可以使用括号将中缀运算符 like 转换+为前缀函数,因此(+) 2 3与 相同2 + 3。Lean中有类似的功能吗?

operators lean

5
推荐指数
1
解决办法
168
查看次数

如何在精益中使用套接字?

如何在 Lean 4 中创建 TCP 套接字并接受传入连接或连接到远程地址?换句话说,如何在 Lean 4 中实现 TCP 服务器或客户端?

sockets lean

5
推荐指数
1
解决办法
556
查看次数