在我的课堂上,我们正在研究C++ 98,所以我试图找到合适的语法.
如何写出声明:
template <class T>
class A{
public:
A();
A(const A &rhs);
A &operator=(const A &rhs);
};
Run Code Online (Sandbox Code Playgroud)
或者应该是这样的:
template <class T>
class A{
public:
A();
A(const A<T> &rhs);
A &operator=(const A<T> &rhs);
};
Run Code Online (Sandbox Code Playgroud)
我想两者的实现是一样的.
它们彼此不同吗?
我将指针传递给rapidjson::Document作为参数。
foo(rapidjson::Document* jsonDocument)
{
std::cout << jsonDocument["name"] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但我无法jsonDocument["name"]访问 name 属性。
尝试不使用指针会导致错误:
error: 'rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>::GenericDocument(const rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; StackAllocator = rapidjson::CrtAllocator]' is private
GenericDocument(const GenericDocument&);
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?
#include <iostream>
using namespace std;
template <typename T>
class test {
public:
T value;
template <typename... Args, typename = decltype(T())>
test(Args... args): value(args...)
{
cout <<"ctor running\n";
}
template <typename... Args>
test(Args...) : value(1)
{
cout <<"ctor unspec running\n";
}
};
class t
{
public:
t() = delete;
explicit t(int) {}
};
int main()
{
test<t> h;
}
Run Code Online (Sandbox Code Playgroud)
我试图为constructor创建的对象(h)调用第二个.我不知道为什么会收到此错误:
Run Code Online (Sandbox Code Playgroud)prog.cc: In function 'int main()': prog.cc:45:13: error: call of overloaded 'test()' is ambiguous test<t> h; ^ prog.cc:25:5: …
这样做有意义吗:
class SomeClass
{
public:
static Object getObject()
{
return Object("example")
}
};
int main()
{
const Object& myObject = SomeClass::getObject();
// do something with myObject
}
Run Code Online (Sandbox Code Playgroud)
或者这与只是获取对象一样
const Object myObject = SomeClass::getObject();
Run Code Online (Sandbox Code Playgroud)
?
共享指针非常聪明.他们记住了最初构造的类型,以便正确删除它们.以此为例:
struct A { virtual void test() = 0; };
struct B : A { void test() override {} };
void someFunc() {
std::shared_ptr<A> ptr1;
ptr1 = std::make_shared<B>();
// Here at the end of the scope, B is deleted correctly
}
Run Code Online (Sandbox Code Playgroud)
然而,void指针似乎存在一个问题:为了使void指针的向下转换有效,必须将它向下转换为它最初上传的类型.
例如:
void* myB = new B;
// Okay, well defined
doStuff(static_cast<B*>(myB));
// uh oh, not good!
// For the same instance of a child object, a pointer to the base and
// a pointer to the child can …Run Code Online (Sandbox Code Playgroud) 我最近偶然发现了将要引入C++ 17标准的std :: is_invocable,我想知道为什么它需要用户为函数指针提供一个类型,而不是只提供函数指针本身,这可能是更方便,特别是因为非类型模板参数现在可以不受约束.
我的意思可以在下面的例子中解释
void hello_world() {
cout << "Hello world" << endl;
}
int main() {
cout << std::is_invocable_v<decltype(hello_world)> << endl;
// as opposed to being able to do
// cout << std::is_invocable_v<hello_world> << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否有人有相同的技巧来找到find_me函数的返回类型,而不更改它的参数.
struct Stuck {
Stuck() = delete;
Stuck(Stuck&&) = delete;
Stuck(const Stuck&) = delete;
Stuck& operator=(Stuck&&) = delete;
Stuck& operator=(const Stuck&) = delete;
};
double find_me(Stuck);
int main() {
// This obviously don't work
decltype(find_me(Stuck{})) test1;
}
Run Code Online (Sandbox Code Playgroud)
这是我试过的另一个镜头:
template<typename T>
struct ConvertTo {
operator T ();
}
int main() {
decltype(find_me(ConvertTo<Stuck>{})) test1;
}
Run Code Online (Sandbox Code Playgroud)
该函数find_me多次重载,从未实际实现过.我只是想知道当函数有这些形式时是否有办法找到返回类型.我知道接收指针或引用是可能的,这就是我现在正在做的事情,但我想知道是否还有一些技巧可以使它工作.
如果有,请告诉我,告诉我原因.
谢谢.
我有以下代码:
helper.hpp:
struct A {
uint32_t a, b;
};
struct B {
uint32_t a, b;
};
template <typename T>
struct C {
T barcode;
};
Run Code Online (Sandbox Code Playgroud)
现在基于某些条件我想在main.cpp中创建适当的struct对象
if(/* something */) {
C<A> obj;
}
else {
C<B> obj;
}
Run Code Online (Sandbox Code Playgroud)
现在问题是因为它在if范围内我无法访问它.处理它的一种方法是从函数返回对象,如下所示:
template <typename T>
C<T> getObject(){
if(/* something */) {
return C<A>{};
}
else{
return C<B>{};
}
}
auto obj = getObject()
Run Code Online (Sandbox Code Playgroud)
但这给了我以下编译错误:
错误:没有用于调用'getObject()注释的匹配函数:无法推导出模板参数'T'
真的很感激任何帮助.
我看过几个类似的代码片段,如下所示:
struct MyExcept : std::exception {
explicit MyExcept(const char* m) noexcept : message{m} {}
const char* what() const noexcept override {
return message;
}
const char* message;
};
void foo() {
std::string error;
error += "Some";
error += " Error";
throw MyExcept{error.c_str()};
}
int main() {
try {
foo();
} catch (const MyExcept& e) {
// Is this okay?
std::cout << e.message << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在注释后面的行中Is this okay?,我们读取了在foo函数中使用分配的c样式字符串std::string.由于字符串是通过堆栈展开来破坏的,这种未定义的行为是什么?
如果它确实是未定义的行为,如果我们main用这个替换函数怎么办?
int main() …Run Code Online (Sandbox Code Playgroud) 我正在检查第一次练习的书cpp模板元编程的一些解决方案 http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?CPPTM_Answers_-_Exercise_2-0
编写一个一元函数add_const_ref,如果它是引用类型则返回T,否则返回T const&
template<typename T>
struct add_const_ref
{
typedef typename boost::add_const<T>::type ct;
typedef typename boost::add_reference<ct>::type type;
};
Run Code Online (Sandbox Code Playgroud)
我用c ++ 11修改了它:
template<typename T>
struct add_const_ref_type
{
typedef typename std::add_const<T>::type ct;
typedef typename std::add_lvalue_reference<ct>::type type;
};
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它适用于参考.我希望这会添加const,即更改int&为`const int&.
int main()
{
std::cout << std::is_same<add_const_ref_type<int &>::type, int&>::value << '\n'; // print 1
std::cout << std::is_same<add_const_ref_type<int &>::type, const int&>::value << '\n'; // print 0
return 0;
}
Run Code Online (Sandbox Code Playgroud)