小编Kan*_* Li的帖子

函数默认模板参数可以放在非默认模板之前吗?

以下代码在gcc-4.7.1上编译:

struct X {};

template <class T = X, typename U>
void f(const U& m) {
}


int main() {
    f<>(0);
}
Run Code Online (Sandbox Code Playgroud)

但是,这个没有:

struct X {};

template <class T = X, typename U>
void f(const U& m) {
    auto g = [] () {};
}


int main() {
    f<>(0);
}
Run Code Online (Sandbox Code Playgroud)

gcc-4.7.1抱怨:

c.cpp: In function 'void f(const U&)':
c.cpp:5:15: error: no default argument for 'U'
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:在函数模板中将非默认参数置于默认参数之前是否正确?如果是的话,为什么不编译第二个呢?如果不是,为什么第一个编译?C++ 11标准如何说明这种语法?

c++ templates c++11

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

如何使初始化列表和类成员初始化程序一起工作?

以下代码既不能在gcc-4.7.1下使用-std = c ++ 11,也不能在clang-3.2下编译.所以我觉得我做错了什么.但我不知道为什么.有人能给我一个暗示吗?基本上,如果我删除X的类内成员初始化程序,它的工作原理.那么为什么初始化列表不能与类内成员初始化器一起使用呢?

struct X {
    int x = 1;
    int y = 1;
};

int main() {
    X x = {1, 2};
}
Run Code Online (Sandbox Code Playgroud)

gcc编译错误:

a.cpp: In function 'int main()':
a.cpp:7:16: error: could not convert '{1, 2}' from '<brace-enclosed initializer list>' to 'X'
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

非const指针更喜欢const T和重载到const T*

假设我有一个函数的两个重载

template <typename T>
void f(const T&) {
    cout << "f(T&)" << endl;
}

template <typename T>
void f(const T*) {
    cout << "f(T*)" << endl;
}
Run Code Online (Sandbox Code Playgroud)

为什么要f(new int)解决f(const T&)而不是f(const T*)?标准中的任何地方都谈到了这种反直觉的行为?

http://ideone.com/kl8NxL

c++ overloading c++11

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

std :: call_once对非原子变量安全吗?

std::call_once正常的非原子变量?请考虑以下代码

std::once_flag once;
int x;

void init() { x = 10; }

void f() {
  std::call_once(once, init);
  assert(x == 10);
}

int main() {
  std::thread t1(f), t2(f);
  t1.join();
  t2.join();
}
Run Code Online (Sandbox Code Playgroud)

返回时是否init会在所有线程中看到副作用call_once?有关cppreference的文档有点模糊.它只表示所有线程std::call_once将在init完成后返回,但没有提到阻止x = 10在init返回后重新排序的任何内容.

有任何想法吗?澄清行为的标准在哪里?

c++ multithreading c++11

9
推荐指数
2
解决办法
593
查看次数

在bash脚本中,如何运行外部定义的bash函数?

例如,以下命令不起作用.我想知道如何解决它,谢谢.

[liuke@liuke-mbp ~]$ function showxx() { echo xx; }
[liuke@liuke-mbp ~]$ showxx
xx
[liuke@liuke-mbp ~]$ cat a.bash 
#!/bin/bash
showxx
[liuke@liuke-mbp ~]$ ./a.bash 
./a.bash: line 2: showxx: command not found
Run Code Online (Sandbox Code Playgroud)

bash

8
推荐指数
1
解决办法
3611
查看次数

在MacPorts中,如何识别和卸载未使用的库端口?

macport中的许多端口作为其他(应用程序)端口的库依赖项安装,但卸载这些应用程序端口不会卸载相应的库依赖项.随着时间的推移,许多这样的库端口变成了orphon,只是浪费空间.所以我想知道是否有办法找到它们.

谢谢.

macos macports

8
推荐指数
2
解决办法
5136
查看次数

为什么gcc不能内联可以确定的函数指针?

以下程序在带有-O3的centos上的gcc 4.6.2下编译:

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

template <typename T>
class F {
public:
     typedef void (T::*Func)();

     F(Func f) : f_(f) {}

     void operator()(T& t) {
         (t.*f_)();
     }
private:
     Func f_;
};

struct X {
    X() : x_(0) {}

    void f(){
        ++x_;
    }

    int x_;
};

int main()
{
     const int N = 100000000;
     vector<X> xv(N);
     auto begin = clock();
     for_each (xv.begin(), xv.end(), F<X>(&X::f));
     auto end = clock();
     cout << end - begin << …
Run Code Online (Sandbox Code Playgroud)

c++ gcc

8
推荐指数
2
解决办法
3369
查看次数

为什么需要转发返回值

在的文档std::forward,给出了以下示例:

template<class T>
void wrapper(T&& arg)
{
    foo(forward<decltype(forward<T>(arg).get())>(forward<T>(arg).get()));
}
Run Code Online (Sandbox Code Playgroud)

为什么在这里需要转发返回值?在什么情况下它与以下代码不同:

template<class T>
void wrapper(T&& arg)
{
    foo(forward<T>(arg).get());
}
Run Code Online (Sandbox Code Playgroud)

c++ perfect-forwarding c++11 c++14

8
推荐指数
1
解决办法
178
查看次数

换行模式std :: begin; 返回开始(c); 成功能

有没有办法将模式包装成一般的模板函数?

template <typename C>
auto Begin(C&& c) -> ??? {
  using std::begin;
  return begin(std::forward<C>(c));
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是如何在这里编写函数的返回类型?

我想要这个的原因是我想写一个模板变量

template <typename C>
constexpr bool IsBidirectionalContainer = 
  std::is_base_of<std::bidirectional_iterator_tag,
                  typename std::iterator_traits<
                      decltype(std::begin(std::declval<C>()))>::iterator_category>::value;
Run Code Online (Sandbox Code Playgroud)

这里的问题是std::begin不会发现的定制化重载beginC通过ADL.如果有人为此做了解决方法,也欢迎.

c++ c++11 c++14 c++17

7
推荐指数
1
解决办法
256
查看次数

嵌套函数中的变量范围

有人可以解释为什么以下程序失败:

def g(f):
  for _ in range(10):
    f()

def main():
  x = 10
  def f():
    print x
    x = x + 1
  g(f)

if __name__ == '__main__':
  main()
Run Code Online (Sandbox Code Playgroud)

随着消息:

Traceback (most recent call last):
  File "a.py", line 13, in <module>
    main()
  File "a.py", line 10, in main
    g(f)
  File "a.py", line 3, in g
    f()
  File "a.py", line 8, in f
    print x
UnboundLocalError: local variable 'x' referenced before assignment
Run Code Online (Sandbox Code Playgroud)

但是,如果我只是将变量更改为x数组,它的工作原理如下:

def g(f):
  for _ in range(10):
    f() …
Run Code Online (Sandbox Code Playgroud)

python scope nested-function

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