我在cppreference.com中发现了一些我不懂的代码。下面是链接:。这是关于dependent template-id的事,我不明白。下面是代码:
//When the result of specializing an alias template is a dependent template-id,
//subsequent substitutions apply to that template-id:
template<typename...>
using void_t = void;
template<typename T>
void_t<
在类模板Foo中,我想检查模板参数是否提供了名为Bar的类型。 struct TypeA {using Bar = int;};
struct TypeB {};
template<class T>Foo{};
void main(){
Foo<TypeA> a; // I want this to compile
Foo<TypeB> b; // I want this to not compile, but print a nice message.
} 因为我想把它和其他属性结合起来,所以我想要一个hasBar元函数。因此,我可以组合布尔值
我主要是一个R程序员,我正在使用Rcpp运行一个由Malte通过RStudio (Microsoft )在Windows 10上运行一个名为RStudio的哈希映射实现。C++编译器是使用c11标志运行的g++。
由于对他的.hpp文件没有任何更改,我无法让它运行,因为它会产生错误
Line 276 no type named 'hash_policy' in 'struct std::hash<char>'
flat_hash_map.hpp中的违规行是
template<typename T>
struct HashPolicySe
我有一个特性可以返回一些关于类的信息,例如维数:
template< typename T >
struct NumDims;
对于“正常”类,这很容易专门化:
template<>
struct NumDims< MyClass >: std::integral_constant< unsigned, 3 >{};
但是,我有一些重模板类,它们都有一个常见的静态类,如:
template< class T, class U, class V, bool x, bool y, bool z >
struct TemplateClass
有一个关于使用别名的on首选项的例子。此示例失败,因为int没有成员foo。
template<typename...> using void_t = void;
template<typename T> void_t<typename T::foo> f();
f<int>(); // error, int does not have a nested type foo
这是显而易见的,但是当我尝试将void_t部件放到参数列表中时,它意外地编译了:
template<typename...> using void_t = void
与类似,是否可以使用SFINAE来确定一个类型是否有一个带有特定参数的成员函数?没有另一个问题中回答的参数列表,说它工作得很好。(我站在后者一边。)但是,如果我尝试使用额外的std::decltype<>()来添加对参数欲望的检查,则会使用template parameters not deducible in partial specialization失败。有没有办法扩展这个方法来检查某些参数类型?
示例代码:
#include <type_traits>
class A {
public :
void a(){}
void b(int val){
我一直试图通过阅读下面的文章来学习SFINAE技巧,但在理解其中的某些部分时遇到了困难。
完整代码:
我主要对这几行代码感到困惑。
// Check if a type has a serialize method.
auto hasSerialize = is_valid([](auto&& x)
-> decltype(x.serialize()) { });
template <class T> auto serialize(T& obj)
-> typename std::enable_if<decltype(
考虑以下代码:
auto f() -> decltype(auto) { /* do whatever you want here */ }
int main() { f(); }
推导了返回类型,并采用decltype(auto)作为后继返回类型。
下面的代码是一个稍微修改(实际上是sfinae'd)的版本:
struct S { static void f() {} };
struct T {};
template<typename U>
auto f(int) -> decltype(U::f(), void()) {
// do whateve