在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++中不存在方法引用.真的吗?如果是,他们不存在的原因是什么?
我写了一个需要Cocoa框架的小helloworld程序.我想知道如何在CMake中添加框架.我发现的所有信息都已过时.我在Snow Leopard上有CMake 2.8.1.
以下程序在 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内部列表没有固定大小的嵌套列表?
请考虑以下示例:
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
我有这个代码(从更复杂的版本简化):
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) 我正在构建一个简单的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系统库的正确(或最佳)方法是什么?
我是一名新的打字稿开发人员,我正在开发一个使用打字稿和 webpack 的项目,一些用 js 编码的库需要 .d.ts 文件,但我很困惑 webpack 如何找到带有 ? 的.d.ts文件import * as PIXI from 'pixi.js'?
这是我的猜测:
webpack会找到“node_modules”命名的文件夹pixi.js,并在其中找到
package.json,如果有该types属性,则选择该文件作为.d.ts文件。
如果按照1的方式找不到.d.ts文件,则进入node_modules/@types(或其他自定义文件夹),查找是否存在同名文件夹pixi.js,并选择index.d.ts文件为文件.d.ts。
module,为什么?我知道官方不推荐这个module关键字(用 代替namespace)。Lean 4 是一种纯粹的函数式编程语言,但它是惰性的(如 Haskell)还是严格的(如 Idris)?这意味着什么?有没有办法选择加入(或选择退出)懒惰?
在 Haskell 中,我可以使用括号将中缀运算符 like 转换+为前缀函数,因此(+) 2 3与 相同2 + 3。Lean中有类似的功能吗?
如何在 Lean 4 中创建 TCP 套接字并接受传入连接或连接到远程地址?换句话说,如何在 Lean 4 中实现 TCP 服务器或客户端?