考虑以下代码:
#include <iostream>
using namespace std;
class A {
private:
int x;
public:
int& get_ref() {
cerr << "non const" << endl;
return x;
}
const int& get_ref() const {
cerr << "const" << endl;
return x;
}
};
int main () {
A a;
a.get_ref() = 10;
cout << a.get_ref() << endl;
const int& y = a.get_ref();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望第二次和第三次调用a.get_ref()运行第二版get_ref()方法(并输出const标准错误).但看起来总是第一个版本被调用.如何实现两个不同的'getter'并确保根据上下文调用正确的版本?即,至少在第三次通话时
const int& y = a.get_ref();
Run Code Online (Sandbox Code Playgroud)
第二个版本执行?(一个不优雅的解决方案是使用不同的名称,例如get_ref,get_const_ref但我试图看看是否可以避免.)
我的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)
但这是不正确的.
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)
我想我已经正确地遵循了指针引用的规则.我仍然不知道为什么会这样.有人能帮助我吗?
我有一个返回容器的函数。我们姑且称之为“ Container”吧。
Container GenerateRandomContainer() { ... }
Run Code Online (Sandbox Code Playgroud)
该函数将生成一个包含每次调用都不同的随机元素的容器。
当我使用 for every 循环迭代此容器时,如下所示:
for(Element e : GenerateRandomContainer()) { ... }
Run Code Online (Sandbox Code Playgroud)
它会生成一个新的Containereach迭代还是在进入foreach循环时只生成一个迭代?
我正在尝试使用模板,但无法理解以下代码有什么问题。
解决.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是抽象类的子类TNLP。Ipopt_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)
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) 我有这个代码:
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)
你能解释一下为什么吗?
如何定义从模板类继承的模板类?
我想包装std::queue并std::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) 我的问题与此类似,但那里没有示例代码,我觉得它没有用.
我的错误是
调用类'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)
我通过两个编译器尝试了这个,并且出现了同样的错误.从标准文档中尽可能多地阅读,但不理解我的错误.
根据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.first是const S*),foo编译的调用很好,因此看起来好像我能够修改指针到const后面的对象.
为什么我能做到这一点?
PS这是Coliru …
c++ ×10
templates ×4
c++11 ×3
pointers ×2
stl ×2
class ×1
const ×1
containers ×1
emplace ×1
foreach ×1
if-statement ×1
inheritance ×1
name-lookup ×1
overloading ×1
polymorphism ×1
ranged-loops ×1
reference ×1
stdmap ×1
string ×1
vector ×1