我试图在不直接知道返回类型的情况下声明std函数.
显然,它将在编译时知道,但我找不到正确的方式来声明它.
最重要的是,我需要创建一个容器,其中包含来自此函数返回的值.
template <typename... Args>
class Sample
{
public:
Sample(Args... args, std::function</*unknown return type*/(Args...)> fnct) :
_inputBuff(std::forward_as_tuple(std::forward<Args>(args)...))
{ }
std::tuple<Args...> _inputBuff;
std::vector</*unknown return type*/> _resultBuff;
};
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?
基类在创建派生类对象时无法查看派生类中定义的宏.[C++ 14]
Base.HPP
class Base {
public:
Base() {
#ifndef SKIP
std::cout << "Bing" << std::endl;
#endif
}
};
Run Code Online (Sandbox Code Playgroud)
文件:Derived.HPP
#define SKIP
class Derived : public Base {
public:
Derived() {}
};
Run Code Online (Sandbox Code Playgroud)
因此,每当我创建Derived类的对象时,我都希望不会Bing在输出终端中看到它,因为我已经定义了宏SKIP.
但这不会发生.似乎Base类对宏的定义一无所知SKIP.有没有办法做到这一点,或者如果不用-DSKIP标志编译代码就不可能做到这一点?
我最近偶然发现了将要引入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) 我正在学习c ++.我想让编译器将nullptr推导为shared_ptr.请阅读以下代码,
struct A {};
struct B {
std::shared_ptr<A> a;
};
struct Tag {
std::shared_ptr<B> b;
};
auto GetSharedPtrClassB(Tag* tag) {
if (tag) {
auto& sharedB = *(tag->b);
return sharedB.a;
} else {
return nullptr; // Error : compiler cannot deduce type of nullptr.
}
}
Run Code Online (Sandbox Code Playgroud)
在GetSharedPtrClassB,nullptr不能推断为std::shared_ptr<A>.错误消息如下,
error: inconsistent deduction for ‘auto’: ‘std::shared_ptr<A>’ and then ‘std::nullptr_t’
Run Code Online (Sandbox Code Playgroud)
我怎样才能让编译器推导出nullptr为std::shared_ptr<A>?我可以提供一种类型decltype(*(tag->b)),但我想不出提供类型的下一步std::shared_ptr<A>.
非常感谢你.
我想知道是否有人有相同的技巧来找到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) After a pointer is initialized, do you have to use the * dereference operator to call the pointer in a condition?
Example:
int main()
{
int var = 10;
int *ptr = &var;
if(ptr) // does this need to be if(*ptr) ???
{.......}
}
Run Code Online (Sandbox Code Playgroud)
And can I have a short explanation as to why?
Thank you.
(我知道这不是常规的实现,但是我想尝试一下。)
struct TrieNode {
std::unordered_map<char, TrieNode> next;
};
Run Code Online (Sandbox Code Playgroud)
此类已很好地编译,并且在Visual Studio 2017下可以按预期工作。但是,它不使用gcc(c ++ 14)进行编译(正如我期望的那样)。
Run Code Online (Sandbox Code Playgroud)In file included from /usr/include/c++/8/bits/stl_algobase.h:64, from /usr/include/c++/8/bits/char_traits.h:39, from /usr/include/c++/8/ios:40, from /usr/include/c++/8/ostream:38, from /usr/include/c++/8/iostream:39, from prog.cpp:1: /usr/include/c++/8/bits/stl_pair.h: In instantiation of ‘struct std::pair<const char, TrieNode>’: /usr/include/c++/8/bits/stl_vector.h:1610:27: required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const char, TrieNode> >’ /usr/include/c++/8/bits/hashtable_policy.h:234:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const char, TrieNode> >’ /usr/include/c++/8/bits/hashtable_policy.h:280:12: required from ‘struct std::__detail::_Hash_node<std::pair<const char, TrieNode>, false>’ /usr/include/c++/8/bits/hashtable_policy.h:2027:49: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const char, TrieNode>, false> > >’ /usr/include/c++/8/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<char, std::pair<const char, TrieNode>, std::allocator<std::pair<const char, …
我想知道单元测试模板是否是一件事。让我解释一下我的需求。
我有一个高度模板化的库。我有很多 sfinae 类型特征,还有一些 static_assert。
我想测试的是 sfinae 类型特征的有效性,并测试我的 static_assert 是否抛出正确的东西。知道我的报道内容会很棒。
这是我的代码的示例:
template<typename T>
using false_v = !std::is_same<T, T>::value;
// Here are my types traits
template<typename T, typename... Args>
struct SomeCondition1 { /* ... */ };
template<typename T, typename... Args>
struct SomeCondition2 { /* ... */ };
// This is a master type trait, that test every others
template<typename T, typename... Args>
using Conditions = std::integral_constant<bool,
SomeCondition1<T, Args...>::value && SomeCondition2<T, Args...>::value
>;
// This is the function that is call when …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×2
c++14 ×2
templates ×2
c++17 ×1
decltype ×1
exception ×1
gcc ×1
if-statement ×1
nested ×1
non-type ×1
pointers ×1
std-function ×1
unit-testing ×1