gcc-4.8接受此代码,但是不是错误,因为非类型参数包相当于void...非法?
template <typename T,
typename std::enable_if<std::is_integral<T>::value>::type...>
void test(T) {}
Run Code Online (Sandbox Code Playgroud)
我用clang-3.5尝试了这个,也接受了它.这是编译器错误,还是我误解了什么?
下面的完整测试代码,它使用非类型的空参数包来简化enable_if.这几乎与Flaming Dangerzone的Remastered enable_if中的内容相同,除非替换后包装成为void....
#include <type_traits>
template < typename C >
using enable_if = typename std::enable_if<C::value>::type ;
template < typename T, enable_if<std::is_integral<T>>... >
void test(T){} // #1
template < typename T, enable_if<std::is_floating_point<T>>... >
void test(T){} //#2
int main()
{
test(0); // calls #1
test(0.0); // calls #2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc-4.8编译上面的代码就好了.clang不是,但那是因为它有一个不同的错误http://llvm.org/bugs/show_bug.cgi?id=11723.
我想存根一个存储库类来测试另一个具有存储库的类(Holder类).存储库接口支持CRUD操作,并且有很多方法,但我对Holder类的单元测试只需要调用其中的两个.存储库界面:
public interface IRepo {
public void remove(String... sarr);
public void add(String... sarr);
//Lots of other methods I don't need now
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个可以存储的情况下,用于定义逻辑库模拟add和remove唯一的,并提供检查什么叫添加和删除后存储在其上的一种手段.
如果我做:
IRepo repoMock = mock(IRepo.class);
Run Code Online (Sandbox Code Playgroud)
然后我有一个愚蠢的对象,对每种方法都没有任何作用.没关系,现在我只需要定义添加和删除的行为.
我可以创建一个Set<String>和存根只有那两个方法来处理集合.然后我将实例化一个具有IRepo的Holder,注入部分存根模拟,并在执行持有者之后,检查该集以验证它包含它应该是什么.
我设法部分存根一个void方法,比如remove使用弃用的方法stubVoid:
Set<String> mySet = new HashSet<>();
stubVoid(repoMock).toAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String[] stringsToDelete = (String[]) args[0];
mySet.removeAll(Arrays.asList(stringsToDelete));
return null;
}
}).on().remove(Matchers.<String>anyVararg());
Run Code Online (Sandbox Code Playgroud)
但是已被弃用,并且它比为IRepo创建部分实现要好得多.有没有更好的办法?
注意:Java 7只应答,这应该在Android中运行.
我正在阅读Ogre3D实现中的一些代码,我无法理解void *类型变量的含义.指针void在C++中意味着什么?
我已经搜索过但找不到任何结果(我的术语可能已关闭)所以请原谅我,如果以前曾经问过这个问题.
我想知道是否有一种简单的方法可以void*在不首先声明函数指针然后为函数指针分配地址的情况下调用C中的函数.
即.假设要调用的函数是typevoid(void)
void *ptr;
ptr = <some address>;
((void*())ptr)(); /* call ptr as function here */
Run Code Online (Sandbox Code Playgroud)
用上面的代码,我得到错误C2066:在VC2008中强制转换为函数类型是非法的
如果可能,具有返回类型和多个参数的函数的语法有何不同?
请考虑以下代码段:
void Foo()
{
// ...
}
void Bar()
{
return Foo();
}
Run Code Online (Sandbox Code Playgroud)
在C++中使用上述内容的正当理由是什么,而不是更常见的方法:
void Foo()
{
// ...
}
void Bar()
{
Foo();
// no more expressions -- i.e., implicit return here
}
Run Code Online (Sandbox Code Playgroud) 在我的C++实现(Visual Studio 2008实现)中,我看到以下行 <cassert>
#ifdef NDEBUG
#define assert(_Expression) ((void)0)
Run Code Online (Sandbox Code Playgroud)
我不明白需要将0转换为void.在我看来,这
#ifdef NDEBUG
#define assert(_Expression) (0)
Run Code Online (Sandbox Code Playgroud)
甚至简单地说
#ifdef NDEBUG
#define assert(_Expression) 0
Run Code Online (Sandbox Code Playgroud)
考虑到assert(expr)可以使用的背景,会做什么.
那么,什么是类型的0危险INT而不是类型0 无效在这种情况下?任何现实的例子?
我看到的一些使用(void*)在printf().
如果我想打印变量的地址,我可以这样做:
int a = 19;
printf("%d", &a);
Run Code Online (Sandbox Code Playgroud)
&a是a地址只是一个整数,对吧?我读过的很多文章都是这样的:
printf("%p", (void*)&a);
Run Code Online (Sandbox Code Playgroud)%p代表什么?(一个指针?)(void*)?我不能用(int)&a吗?如果需要返回一个VoidJavadoc描述为的类型
一个类,它是一个不可实例化的占位符类,用于保存对表示Java关键字void的Class对象的引用.
为什么以下仍需要null退回?
public Void blah() {
return null; // It seems to always want null
}
Run Code Online (Sandbox Code Playgroud) 我在java中编写这个简单的类只是为了测试它的一些功能.
public class class1 {
public static Integer value=0;
public class1() {
da();
}
public int da() {
class1.value=class1.value+1;
return 5;
}
public static void main(String[] args) {
class1 h = new class1();
class1 h2 = new class1();
System.out.println(class1.value);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
2
但是在这段代码中:
public class class1 {
public static Integer value=0;
public void class1() {
da();
}
public int da() {
class1.value=class1.value+1;
return 5;
}
public static void main(String[] args) {
class1 h = new class1();
class1 h2 = …Run Code Online (Sandbox Code Playgroud) 这是问题的一个后续:什么是void()中decltype(void())意味着什么?.
decltype(void())编译好,void()在这种情况下的手段在上述问题中解释(实际上在答案中).
另一方面,我注意到decltype(void{})没有编译.
它们之间有什么区别(decltype至少在上下文中)?
为什么第二个表达式不能编译?
为了完整起见,它遵循一个最小的(不是)工作示例:
int main() {
// this doesn't compile
//decltype(void{}) *ptr = nullptr;
// this compiles fine
decltype(void()) *ptr = nullptr;
(void)ptr;
}
Run Code Online (Sandbox Code Playgroud)