小编cpp*_*ner的帖子

向前将结构声明为类时出现Visual C ++ 2015链接器错误

我有以下代码(涉及多个文件)...

//--- SomeInterface.h
struct SomeInterface
{
  virtual void foo() = 0;
  virtual ~SomeInterface(){}
};

//--- SomeInterfaceUser.h
#include <memory> //shared_ptr

class SomeInterface;
//NOTE: struct SomeInterface... causes linker error to go away...

class SomeInterfaceUser
{
  public:
    explicit SomeInterfaceUser(std::shared_ptr<SomeInterface> s);
};

//SomeInterfaceUser.cpp
#include "SomeInterfaceUser.h"
#include "SomeInterface.h"
SomeInterfaceUser::SomeInterfaceUser(std::shared_ptr<SomeInterface> s)
{
}

//SomerInterfaceUserInstantiator.cpp
#include "SomeInterfaceUser.h"
#include "SomeInterfaceImpl.h"

struct SomeInterfaceImpl : SomeInterface
{
  virtual void foo(){}
};

void test()
{
  SomeInterfaceUser x{std::make_shared<SomeInterfaceImpl>()};
}
Run Code Online (Sandbox Code Playgroud)

使用Visual C ++编译器,我得到一个链接器错误(LNK2019)。使用GCC 4.8.4并非如此。将前向声明类SomeInterface更改为struct SomeInterface可使链接器错误消失。我一直以为应该可以互换使用类/结构?SomeInterfaceUser的接口不应取决于SomeInterface是定义为类还是struct,不是吗?

这是Visual C ++错误吗?我找不到任何与此有关的东西。我怀疑将struct用作模板参数这一事实与它有关。

感谢您的帮助。

c++ visual-c++

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

如何打破循环的opemp?

我有一个非常ᴄᴘᴜ密集(50万次调用和超过100亿次循环阶段)的循环,如:

for(int i=0;i<*string;i++){
    if(!check_some_stuff(string+i)) {
        do_some_stuff(i,string-2);
        if(!string)
            break;
       do_yet_other_stuff(string);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于#pragma omp parallel for odered我认为不能使用break语句,因此我可以将其设置i为非常大的值.

for(int i=0;i<*string;i++){
    if(!check_some_stuff(string+i)) {
        do_some_stuff(i,string-2);
        if(!string)
            i=0x7FFFFFFB;
       do_yet_other_stuff(string);
    }
}
Run Code Online (Sandbox Code Playgroud)

没有openmp就完美无缺.但是当我添加时

#pragma omp parallel for ordered shared(string)
for(int i=0;i<*string;i++){
    if(!check_some_stuff(string+i)) {
        do_some_stuff(i,string-2);
        #pragma omp critical
        if(!string)
            i=0x7FFFFFFB; // it seems the assignment has no effect on the value of i.
       do_yet_other_stuff(*string);
    }
}
Run Code Online (Sandbox Code Playgroud)

i似乎没有改变,所以它变成了一个无限循环.

c for-loop openmp

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

`std :: lock_guard <std :: mutex>`对象没有名称时的不同行为

我正在学习std::mutex,std::thread我对下面两段代码的不同行为感到惊讶:

#include <iostream>
#include <mutex>
#include <thread>
using namespace std;

std::mutex mtx;

void foo(int k)
{
    std::lock_guard<std::mutex> lg{ mtx };
    for (int i = 0; i < 10; ++i)
        cout << "This is a test!" << i << endl;
    cout << "The test " << k << " has been finished." << endl;
}

int main()
{
    std::thread t1(foo, 1);
    std::thread t2(foo, 2);
    t1.join();
    t2.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是顺序的.但如果我没有名称变量std::lock_guard<std::mutex>,则输出无序

void foo(int k)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 stdthread stdmutex

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

空卷括号{}作为范围的结尾

我正在Yosemite的XCode上运行.

代码编译但在运行时崩溃,为什么?

我故意在第二个std :: copy中使用"{}"作为"范围结束".

我试验了这段代码,因为一个工作示例使用"{}"作为"默认构造的流迭代器作为范围的结尾".

那么为什么(见第二个代码)一个正在工作,但这个(第一个代码)一个失败了?

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

int main()
{
    vector<int> coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // copy the elements of coll1 into coll2 by appending them
    vector<int> coll2;
    copy (coll1.cbegin(), coll1.cend(),    // source
          back_inserter(coll2));           // destination

    vector<int> coll3;
    copy (coll1.begin(), {},
          back_inserter(coll3));

}
Run Code Online (Sandbox Code Playgroud)

以下代码来自The C++ Standard Library第二版.

带有"// end of source"的行可以是"istream_iterator()",也可以是"{}",

两者都有效,因为:引用了这本书

"请注意,从C++ 11开始,您可以将空的花括号而不是默认构造的流迭代器作为范围的结尾传递.这是有效的,因为定义源范围结束的参数类型是从前一个参数推导出来的它定义了源范围的开始."

/* The following code example is …
Run Code Online (Sandbox Code Playgroud)

c++ iterator c++11

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

无法创建静态constexpr

我有以下代码:

struct Literal
{
    int val;
    constexpr Literal(int const& val) : val(val) {}
    constexpr Literal(Literal const& rhs) : val(rhs.val) {}
};


struct Parent
{
    struct StaticObject
    {
        Literal const zero;

        constexpr StaticObject() :zero(0) {}
    };
    static constexpr StaticObject outer{};
};
Run Code Online (Sandbox Code Playgroud)

行'static constexpr StaticObject outer {};' 给我错误:

'表达式没有评估为常数'

接下来是

注意:失败是由调用未定义的函数或未声明'constexpr'的函数引起的

注意:请参阅'Parent :: StaticObject :: StaticObject'的用法

据我所知,这里使用的所有函数都是定义和声明的constexpr.我错过了什么,或者这是一个编译器错误?

c++ constexpr c++11

5
推荐指数
0
解决办法
95
查看次数

具有显式模板实例化的extern关键字

考虑我的问题的一个小的精简用例,其中我有一个标题如下

#include <iostream>
#pragma once
#ifndef HEADER_H
#define HEADER_H
template<typename T>
class FOO
{
public:
    void func() { std::cout << "Foo!"; };
};

extern template class __declspec(dllexport) FOO<int>;
using myfoo = FOO<int>;
#endif
Run Code Online (Sandbox Code Playgroud)

和一个源文件为

#include "Header.h"

int main()
{
    myfoo f;
    f.func();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我使用VS 2015(更新3)进行编译时,出现以下警告

warning C4910: 'FOO<int>': '__declspec(dllexport)' and 'extern' are incompatible on an explicit instantiation
Run Code Online (Sandbox Code Playgroud)

MSDN page does not explain to me clearly why is it wrong to have extern and dllexport in explicit template …

c++ c++11 visual-studio-2015

5
推荐指数
0
解决办法
856
查看次数

如何实现构造函数,以便它只接受使用typeid的输入迭代器?

我想为某个对象实现一个范围构造函数,但我想将它限制为只接受两个输入迭代器.

我试图用gcc 7.1.0编译这段代码.

文件 test.cpp

#include <vector>
#include <type_traits>
#include <typeinfo>

template <typename Iterator>
using traits = typename std::iterator_traits<Iterator>::iterator_category;

template <typename T>
class A{
   private:

      std::vector<T> v;

   public:

      template <typename InputIterator,
               typename = std::enable_if_t<
                  typeid(traits<InputIterator>) ==
                  typeid(std::input_iterator_tag)>
               >
      A(InputIterator first, InputIterator last) : v(first, last) {}
};

int main(){
   std::vector<double> v = {1, 2, 3, 4, 5};
   A<double> a(v.begin(), v.end());
}
Run Code Online (Sandbox Code Playgroud)

我得到这个编译错误g++ test.cpp -o test:

  test.cpp: In function ‘int main()’:
  test.cpp:27:34: error: no matching function for call to …
Run Code Online (Sandbox Code Playgroud)

c++ constructor iterator sfinae c++14

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

必须"定义"宏检查并且其调用是在不同的条件下吗?

我很难在SO上找到许多引用这个主题的答案,但这里有一些看似无辜的代码,当F它不是一个定义的宏时无法编译,

int main() {
#if defined(F) && F(0, 2, 0)
  return 0;
#endif
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

根据GCC手册的这一部分,问题是内部#if表达式"表达式中的所有宏都在表达式值的实际计算开始之前被扩展",所以这在无效检查中,因为当F未定义时,我看到,

test.cpp:2:20: error: missing binary operator before token "("
 #if defined(F) && F(0, 2, 0)
                    ^
Run Code Online (Sandbox Code Playgroud)

我的问题:这是正确做这种检查的唯一方法吗?

int main() {
#if defined(F)
#if F(0, 2, 0)
  return 0;
#endif
#endif
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

我发现这很丑陋且不直观,所以我希望在预处理器中有更好的方法来做这些事情.

c++ c-preprocessor

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

矩阵推导

我在寻找初始化矩阵的推导指南。

我试图在包使用包和sizeof...initializer_list<initializer_list<T>>为阵列的结构,但没有自定义类是作品?

所以,我正在寻找初始化

template <class T, size_t s1, size_t s2>
class matrix
{
T matr[s1][s2]; //maybe with custom array class, if this problem need this
public:
//constructor
};
//deductor
Run Code Online (Sandbox Code Playgroud)

喜欢

matrix m{{1, 2}, {1, 2}};
Run Code Online (Sandbox Code Playgroud)

要么

matrix m({1, 2}, {1, 2});
Run Code Online (Sandbox Code Playgroud)

c++ matrix template-argument-deduction c++17

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

从变换切换到过滤器时,size() 会导致错误

什么时候

  • views::filter([](auto n) { return n % 2 == 0; });已激活,我无法获取[2,4]
  • views::transform([](auto n) { return 2 * n; });已激活,我成功获得了[2,4,6,8,10]
#include <iostream>
#include <ranges>
#include <sstream>

int main()
{
    using namespace std;
    auto input = views::iota(1, 5 + 1);

    auto output = input |
                  views::filter([](auto n) { return n % 2 == 0; });
                  //views::transform([](auto n) { return 2 * n; });


    stringstream ss;
    ss << "[";
    for (auto i = 0; i < output.size() …
Run Code Online (Sandbox Code Playgroud)

c++ std-ranges

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