小编son*_*yao的帖子

static_assert和类模板

我的static_assert功能有问题.当我直接实例化一个类模板时,一切都按预期工作.但是当我将它作为不同类模板的参数传递时,static_assert不起作用.

template <int X>
class A{
    static_assert(X == 0, "X != 0");
};

template <class T>
class B{

};

B<A<1>> b;           // Static assert does not work
A<1>    a;           // error: static assertion failed: X != 0
Run Code Online (Sandbox Code Playgroud)

编辑

谢谢大家的答案.有没有办法显式实例化A而不创建A实例/从A继承?我在尝试这个:

template <int X>
class A{
    static_assert(X == 0, "X != 0");
};

template <class T>
class B;

template <template <int X> class T, int X>
class B<T<X>>{
    template class T<X>;
};
Run Code Online (Sandbox Code Playgroud)

但这是不正确的.

c++ templates static-assert c++11

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

解决"表达式必须具有指向类类型的指针"错误

class testClass { public: int B, C; };

testClass u;
testClass * k = &u;
testClass ** m = &k;

*m->B = 1;//Error appears here
Run Code Online (Sandbox Code Playgroud)

我想我已经正确地遵循了指针引用的规则.我仍然不知道为什么会这样.有人能帮助我吗?

c++ pointers operator-precedence

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

for every 循环中的语句是否每次迭代都执行?

我有一个返回容器的函数。我们姑且称之为“ Container”吧。

Container GenerateRandomContainer() { ... }
Run Code Online (Sandbox Code Playgroud)

该函数将生成一个包含每次调用都不同的随机元素的容器。

当我使用 for every 循环迭代此容器时,如下所示:

for(Element e : GenerateRandomContainer()) { ... }
Run Code Online (Sandbox Code Playgroud)

它会生成一个新的Containereach迭代还是在进入foreach循环时只生成一个迭代?

c++ foreach containers c++11 ranged-loops

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

模板类专业化:模板 ID 不匹配任何模板声明

我正在尝试使用模板,但无法理解以下代码有什么问题。

解决.h

#include "nlp.h"
#include "Ipopt_solve.h"

enum algo_type {IPOPT =1, SQP};

template<int ALG>
class solve
{
public:
    solve()
    {
    }
};

template<>
class solve<IPOPT>
{
public:
    solve(nlp*);

private:
    Ipopt_solve m_ipopt;

};
Run Code Online (Sandbox Code Playgroud)

解决.cpp

template<>
solve<IPOPT>::solve(nlp* problem): m_ipopt(problem)
{
}
Run Code Online (Sandbox Code Playgroud)

Ipopt_solve是抽象类的子类TNLPIpopt_solve使用对nlp类的引用进行初始化。

来自 main.cpp

nlp problem(&model);
solve<IPOPT> solution(&problem);
Run Code Online (Sandbox Code Playgroud)

我收到如下所示的错误。

错误:模板 id 'solve<>' for 'solve<1>::solve(nlp*)' 不匹配任何模板声明 solve::solve(nlp* problem): m_ipopt(problem)

c++ templates template-specialization

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

Return both string and integer from a template function in C++

I am trying to write a generic function, using template, that is able to return bool, integer or char* or string.

template<typename entryType>
entryType getEntry(xml_node node, const char* entryKey)
{
    ...

    if (is_same<entryType, bool>::value) {
         return boolvalue; //returning a boolean
    }
    else if(is_same<entryType, int>::value) {
         return a; //this is an integer
    }
    else if (is_same<entryType, char*>::value) {
         return result; //this is a char*
    }
}
Run Code Online (Sandbox Code Playgroud)

and I would like to be able to call it like:

bool bPublisher = getEntry<bool>(node, …
Run Code Online (Sandbox Code Playgroud)

c++ templates if-statement

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

std::vector emplace 和 std::vector emplace 返回成对

我有这个代码:

std::vector<std::pair<const std::string, int>> vec;

vec.emplace_back("a", 1); //success
vec.emplace(vec.end(), "b", 2); //compile error

vec.emplace_back(std::make_pair<const std::string, int>("c", 3));  //success
vec.emplace(vec.end(),
     std::make_pair<const std::string, int>("d", 4)); //compile error
Run Code Online (Sandbox Code Playgroud)

你能解释一下为什么吗?

c++ stl vector c++11 emplace

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

C ++模板类继承

如何定义从模板类继承的模板类?

我想包装std::queuestd::priority_queue转到基类。就我而言LooperQueue。我用StdQueue这种方式auto queue = new StdQueue<LooperMessage *>()

我的课定义编译器抱怨

错误日志:

  In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
  /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:14:5: error: unknown type name 'size_type'; did you mean 'size_t'?
      size_type size() override;
      ^~~~~~~~~
      size_t
  /Users/rqg/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/5.0.300080/include/stddef.h:62:23: note: 'size_t' declared here
  typedef __SIZE_TYPE__ size_t;
                        ^
  In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
  /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:16:5: error: unknown type name 'reference'
      reference front() override;
      ^
  /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:20:21: error: unknown type name 'value_type'; did you mean 'ARect::value_type'?
      void push(const value_type &x) override;
                      ^~~~~~~~~~
                      ARect::value_type …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates class name-lookup

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

通过string :: insert插入char时出错

我的问题与类似,但那里没有示例代码,我觉得它没有用.

我的错误是

调用类'std :: __ 1 :: __ wrap_iter'的私有构造函数

我的问题的一个最小例子可以在下面找到:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string g = "this_word";
    cout << g << endl;
    char temp = g[0];
    g.erase(0,1);
    cout << g << endl;
    g.insert(0,temp); // Compiler seems to dislike this.  Why?
    cout << g << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我通过两个编译器尝试了这个,并且出现了同样的错误.从标准文档中尽可能多地阅读,但不理解我的错误.

c++ string

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

为什么我可以将std :: map的键传递给期望非const的函数?

根据std::map 文档,它存储键值对std::pair<const Key, Value>,因此映射中的键是const.

现在假设我有一个std::map键是指向某些对象的指针.

struct S {};
struct Data {};
using MyMap = std::map<S*, Data>;
Run Code Online (Sandbox Code Playgroud)

我们还假设有一个foo接受S*参数的函数.

void foo(S* ptr) { /* modify the object (*ptr) */ }
Run Code Online (Sandbox Code Playgroud)

现在,问题是:当我MyMap使用基于范围的for循环迭代时,我能够将map元素键传递给foo:

MyMap m = getMyMapSomehow();
for (auto elem : m)
{
    static_assert(std::is_const<decltype(elem.first)>::value, "Supposed to be `const S*`");
    foo(elem.first); // why does it compile?
}
Run Code Online (Sandbox Code Playgroud)

所以,即使我的static_assert成功(所以我假设它的类型elem.firstconst S*),foo编译的调用很好,因此看起来好像我能够修改指针到const后面的对象.

为什么我能做到这一点?

PS这是Coliru …

c++ pointers stl const stdmap

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

显示向量的所有元素

第一次for循环

  国际我;
  for (i = 0; i <= vec.size(); i++) {
    如果 (vec.size() == 0) {
      cout << "[] 列表为空" << endl;
    } 别的 {
      cout << vec[i] << " ";
    }
  }
}

第二个for循环

cout << "[ ";
for(自动编号:vec)
  cout << num << " ";
cout<<“]”;

为什么当我使用第一个for循环显示向量中的所有元素时,我在向量末尾得到“0”。但是,当我使用范围 for 循环时,我没有得到零。

c++ for-loop vector c++11

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