小编lin*_*bin的帖子

为什么使用-pthread使用-lpthread隐式声明pthread_yield?

我使用gcc在CentOS7中编译此代码main.c:

#include <pthread.h>
void* mystart(void* arg)
{
    pthread_yield();
    return(0);
}
int main(void)
{
    pthread_t pid;
    pthread_create(&pid, 0, mystart, 0);
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

第一次编译:gcc -Wall -g main.c -pthread -o a.out
一切都好.

第二次编译:gcc -Wall -g main.c -lpthread -o a.out

警告:函数'pthread_yield'的隐式声明[-Wimplicit-function-declaration]

  1. 第二个a.out还可以正常运行吗?
  2. 如何修复警告-pthread?是sched_yield另一种产生pthread的方法吗?

c linux gcc pthreads

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

指针的C++模板专业化?

我读了书<C++模板 - 完整指南>和学习指针的模板专业化.(也许我误解了这本书的这一部分)

(1)这是我的简单模板:

#include <iostream>

template<typename T>
void Function(const T& a)
{
    std::cout << "Function<T>: " << a << std::endl;
}

template<typename T>
void Function<T*>(const T* a)
{
    std::cout << "Function<T*>: " << a << std::endl;
}

int main(void)
{
    Function(1);
    Function(1.2);
    Function("hello");
    Function((void*)0x25);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我使用ubuntu16.04 x64,g ++ 5.3,编译器报告:

$ g++ main.cpp -o main.exe 
main.cpp:10:29: error: non-type partial specialization ‘Function<T*>’ is not allowed
 void Function<T*>(const T* a)
Run Code Online (Sandbox Code Playgroud)

(2)但这段代码是正确的:

#include <iostream>

template<typename T>
void Function(const T& a)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
2
解决办法
4414
查看次数

C++ template-id与任何模板都不匹配?

我用模板和专业编写了一个简单的代码:

#include <iostream>

template <class T>
int HelloFunction(const T& a)
{
    std::cout << "Hello: " << a << std::endl;
    return 0;
}

template <>
int HelloFunction(const char* & a)
{
    std::cout << "Hello: " << a << std::endl;
    return 0;
}

int main()
{
    HelloFunction(1);
    HelloFunction("char");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我认为char*的专业化是正确的,但是g ++报告:

D:\work\test\HelloCpp\main.cpp:11:5: 
error: template-id 'HelloFunction<>' for 'int HelloFunction(const char*&)' does not match any template declaration
Run Code Online (Sandbox Code Playgroud)

请帮我找到这个bug.

c++ templates

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

使用移动构造函数时将 self 重置为 nullptr 是一个好习惯吗?

在 C++11 中,移动构造函数/运算符支持资源/内存移动。

这是我的例子:

class A {
public:
    A() : table_(nullptr), alloc_(0) {}
    ~A()
    {
        if (table_)
            delete[] table_;
    }

    A(const A & other)
    {
        // table_ is not initialized
        // if (table_)
        //    delete[] table_;
        table_ = new int[other.alloc_];
        memcpy(table_, other.table_, other.alloc_ * sizeof(int));
        alloc_ = other.alloc_;
    }
    A& operator=(const A & other)
    {
        if (table_)
            delete[] table_;
        table_ = new int[other.alloc_];
        memcpy(table_, other.table_, other.alloc_ * sizeof(int));
        alloc_ = other.alloc_;
        return *this;
    }

    A(A && other)
    {
        // table_ …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

在Win7中使用MinGW中的pthreads

2015-1-20,我在Win7中使用MinGW,尝试使用MinGW和Pthreads编译我的源代码.MinGW已经支持POSIX线程,在"MinGW Installation Manager"中,我们可以安装pthreads dev包和pthreads lib.但是当我编译我的源代码时,有一个错误:struct timespec重新定义,首先在pthread.h中,然后在unistd.h中,我不知道为什么.

在此之前,我实际上安装了没有pthreads dev和lib包的minGW,我下载了pthreads-for-win32源代码并在本地构建它.我使用-I/somewhere -L/somewhere -lpthreadGC2链接到我的源代码,它工作正常.

但这一次,MinGW自包装失败了.我需要帮助.我无法上传图片来向您显示详细信息....

mingw pthreads

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

C++如何将数组int [10] push_back转换为std :: vector <int [10]>?

#include <vector>
using namespace std;

vector<int[60]> v;
int s[60];
v.push_back(s);
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2015社区中的此代码报告编译错误:

错误(活动)没有重载函数的实例"std :: vector <_Ty,_Alloc> :: push_back [with _Ty = int [60],_ Alloc = std :: allocator]"匹配参数列表

错误C2664'void std :: vector> :: push_back(const int(&)[60])':无法将参数1从'int'转换为'int(&&)[60]'

c++ arrays stl vector

4
推荐指数
2
解决办法
2837
查看次数

在 FreeBSD 11.2 中找不到 clang-format

我安装了一个 FreeBSD 虚拟机,然后我运行了sudo pkg install clang-devel.

clang-format 然而,似乎缺少:

-sh:clang 格式:未找到

如何clang-format在 FreeBSD 11.2 中安装?

freebsd clang-format

4
推荐指数
1
解决办法
945
查看次数

将 org.apache.avro.util.Utf8 转换为 java.lang.String

我用 avro 库编写了一个示例。它User从 schema生成 java 类源代码record User

我想编码Userbyte[],然后解码byte[]User

解码时,我收到错误消息:

java.lang.ClassCastException: org.apache.avro.util.Utf8 cannot be cast to java.lang.String
Run Code Online (Sandbox Code Playgroud)

如何转换org.apache.avro.util.Utf8为java String

java avro

4
推荐指数
1
解决办法
6653
查看次数

使用pip或dnf在Fedora中安装python包?

当我在Fedora中安装一些python包时,有两种方法:

  • 使用 dnf install python-package
  • 使用 pip install package

我注意到即使我使用dnf update最新的Fedora,当我使用pip时,它仍然会告诉我类似的东西

pip是旧版本,请使用pip update

我猜dnf包管理与python-pip包管理不同.

那么更推荐哪一个安装python包?

python pip fedora yum dnf

3
推荐指数
1
解决办法
3832
查看次数

如何在另一个once_cell::sync::Lazy 中使用once_cell::sync::Lazy?

我想使用Rust和once_cell来实现一些静态const结构实例,并且一个静态const向量包含这些静态结构实例。

这是示例代码:

use once_cell::sync::Lazy;

pub struct Kind {
  name: String,
  value: i32,
}

impl Kind {
  pub fn new(name: &str, value: i32) -> Kind {
    Kind {name: String::from(name), value}
  }
}

const HYBRID: Lazy<Kind> = Lazy::new(|| Kind::new("a", 1));
const BOND: Lazy<Kind> = Lazy::new(|| Kind::new("b", 2));
// other instances

static KINDS: Lazy<Vec<Kind>> = Lazy::new(|| {
  vec![
    HYBRID,
    BOND,
    // other instances,
  ]
});
Run Code Online (Sandbox Code Playgroud)

这是编译器错误:

use once_cell::sync::Lazy;

pub struct Kind {
  name: String,
  value: i32,
}

impl Kind {
  pub …
Run Code Online (Sandbox Code Playgroud)

synchronization lazy-initialization rust

3
推荐指数
1
解决办法
1581
查看次数