- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当我尝试像这样编写自己的decay_t
时:
#include <utility>
template <class T>
struct auto_decay {
auto operator () () noexcept {
return std::declval<T>();
}
};
template <class T> using decay_t = decltype( (decl_as<decay_impl::auto_decay< T >>())() );
并使用以下方法对其进行测试:
#include <type_traits>
int main() {
static_assert(is_same<decay_t<int ()&>, int (*)()>{}());
}
我遇到了以下错误:
In file included from test_decay.cc:1:
In file included from ./../src/decay.hpp:4:
In file included from ./../src/utility.hpp:3:
./../src/add_reference.hpp:17:14: error: reference to function type 'int () &' cannot have '&' qualifier
typedef T&& type;
^
./../src/add_reference.hpp:20:1: note: in instantiation of template class 'nxwheels::concept_check::add_rvalue_reference<int () &>' requested here
using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
^
./../src/utility.hpp:14:1: note: in instantiation of template type alias 'add_rvalue_reference_t' requested here
auto declval() noexcept -> add_rvalue_reference_t<T>;
^
./../src/decay.hpp:10:16: note: while substituting explicitly-specified template arguments into function template 'declval'
return declval<T>();
^
./../src/decay.hpp:16:46: note: in instantiation of member function 'nxwheels::concept_check::decay_impl::auto_decay<int () &>::operator()' requested here
template <class T> using decay_t = decltype( (decl_as<decay_impl::auto_decay< T >>())() );
^
test_decay.cc:17:17: note: in instantiation of template type alias 'decay_t' requested here
assert_same<decay_t<int (void)&>, int (*)(void)>();
^
In file included from test_decay.cc:1:
./../src/decay.hpp:10:16: error: call to function 'declval' that is neither visible in the template definition nor found by argument-dependent lookup
return declval<T>();
^
./../src/decay.hpp:16:46: note: in instantiation of member function 'nxwheels::concept_check::decay_impl::auto_decay<int () &>::operator()' requested here
template <class T> using decay_t = decltype( (decl_as<decay_impl::auto_decay< T >>())() );
^
test_decay.cc:17:17: note: in instantiation of template type alias 'decay_t' requested here
assert_same<decay_t<int (void)&>, int (*)(void)>();
^
./../src/utility.hpp:14:6: note: 'declval' should be declared prior to the call site
auto declval() noexcept -> add_rvalue_reference_t<T>;
^
In file included from test_decay.cc:2:
./../src/concepts/is_same.hpp:18:5: error: static_assert failed
static_assert(is_same_v<T1, T2>);
^ ~~~~~~~~~~~~~~~~~
test_decay.cc:17:5: note: in instantiation of function template specialization 'nxwheels::concept_check::assert_same<int, int (*)()>' requested here
assert_same<decay_t<int (void)&>, int (*)(void)>();
^
3 errors generated.
但是当我把测试改成:
#include <type_traits>
int main() {
static_assert(is_same<decay_t<int (&)()>, int (*)()>{}());
}
然后测试就过去了,一直看不懂int(&)()
和int()&
有什么区别。我检查了 std::is_funcion和 Declaring functions ,两者都表示 int () &
,而不是 int (&)()
。
编辑:
顺便说一句,在 std::is_funcion 的可能的实现
部分, Ret (Args...) &
是 std::is_function 的特化。他们会怎么做?
最佳答案
R(&)(Arg...)
是对函数的引用, 而 R(Arg...)&
是声明的成员函数的符号
struct test {
void func() &;
};
编辑:
#include <type_traits>
struct test {
void func1() &;
};
template<typename>
struct PM_traits {};
template<class T, class U>
struct PM_traits<U T::*> {
using member_type = U;
};
int main()
{
using P = PM_traits<decltype(&test::func1)>::member_type;
static_assert(std::is_function<P>::value);
}
这要归功于支持 R(Arg...)&
的 std::is_function
的重载
关于c++ - Ret (&)(Args...) 和 Ret (Args...) & 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50990511/
我有这个代码。为了让它工作,我必须使用 Args&&... 而不是 Args... 或 Args&... 我注意到 args 从 & 转换为 const& 或 && 转换为 &。 Args...Arg
当我定义类时,我总是去 Class A(object): def __init__(self, arg): self.arg = arg def print_arg(s
假设我想定义两个 {Type} 类的变量。构造函数采用 1 个参数。下面两种方式是否完全等价(编译成相同的目标代码)? Type a(arg), b(arg); 和 Type a(arg); Type
(旁白:我是一名 Perl 程序员,正如您所知,这是我的第一个重要的 Java 程序。简单的术语将不胜感激。) 我有以下启动器作为编码工作: import java.lang.reflect.*; i
Math.nextUp(arg) 始终与 arg + Math.ulp(arg) 相同,还是我遗漏了什么? System.out.println( 0.5 + Math.ulp(0.5));
今天我在学习完美转发,我创建了这个代码示例 #include #include template auto toStdFun(Function&& fun, Args&&...ar
我想知道你会选择哪个选项? putStrLn (show randomNum) putStrLn $ show randomNum (putStrLn . show) randomNum 所有选项在语
我试图在 visual studio 2012 中编译一个库,它最初是用 c++ 为 visual studio 2015 编写的。我有一个错误说 'class' missing tag。 错误消息的
我在下面的代码中遇到了运行时异常ArrayIndexOutOfBoundException,行中: if ( args[0].equals("t") || args[0].equals("time")
我有以下代码 import React, { Component } from "react"; import { Accounts } from "meteor/accounts-base"; ex
这个问题已经有答案了: Difference between Arrays and 3 dots (Varargs) in java (3 个回答) 已关闭 5 年前。 受学校线性代数 I 和 II
所以我定义了一个函数: def getDistnace(self, strings, parentD, nodeName, nodeDistance): 我用它来调用: Node.getDistnac
这个问题在这里已经有了答案: subprocess.call() arguments ignored when using shell=True w/ list [duplicate] (2 个答案
我想将参数传递给 java 应用程序,但喜欢 linux 应用程序风格。 java 中的main 方法对所有参数使用一个String 数组。在 Linux 中,大多数应用程序接受如下参数:ls -l
这是我的代码片段 #include void change(int a[]){ printf("%p\n",&a); } int main(){
我需要使用 python 3.6 subprocess.run() 函数发出以下命令: gsettings set org.gnome.shell enabled-extensions "['appl
这两个函数是否有任何有意义的不同?有什么理由通常更喜欢一个而不是另一个吗? void foo(auto x, auto &... y) { /* ... */ } template void foo(
例如: def m(arg, ...args) { println "arg: $arg" println "args: $args" } m('arg', k:'v') 输出: ar
我对 Java 还很陌生。目前正在尝试将 args[] 中给出的文件名传递给此 FileReader,但当我编译时,它说找不到指定的文件。如果我对文件名进行硬编码,它就可以正常工作。这应该如何运作?
为什么这是一个语法错误??做这件事的合适方法是什么? >>> def f(*args, option=None): File "", line 1 def f(*args, option=
我是一名优秀的程序员,十分优秀!