为了看看它是如何工作的,我查看std::common_type了头文件中的libstdc ++实现type_traits.我不得不承认我并不真正理解它是如何运作的.这里是:
/// common_type
template<typename... _Tp>
struct common_type;
template<typename _Tp>
struct common_type<_Tp>
{ typedef _Tp type; };
template<typename _Tp, typename _Up>
struct common_type<_Tp, _Up>
{ typedef decltype(true ? declval<_Tp>() : declval<_Up>()) type; };
template<typename _Tp, typename _Up, typename... _Vp>
struct common_type<_Tp, _Up, _Vp...>
{
typedef typename
common_type<typename common_type<_Tp, _Up>::type, _Vp...>::type type;
};
Run Code Online (Sandbox Code Playgroud)
我很清楚第一,第二和第四声明如何运作.但是,我无法理解第三个声明的工作原理.有人可以尝试解释这里使用的机制吗?
全部 -
我正在按照此页面上最简单的说明操作:
http://ant.apache.org/manual/develop.html
但是,当我尝试执行目标"main"时,我在netbeans中遇到此错误:
Run Code Online (Sandbox Code Playgroud)taskdef class dec102012.MyAntTask cannot be found using the classloader AntClassLoader[]
但是这个错误没有意义,因为扩展"Task"的新Java类看起来像这样:
package dec102012;
import org.apache.tools.ant.BuildException;
public class MyAntTask extends org.apache.tools.ant.Task{
private String msg;
// The method executing the task
public void execute() throws BuildException {
System.out.println(msg);
}
// The setter for the "message" attribute
public void setMessage(String msg) {
this.msg = msg;
}
}
Run Code Online (Sandbox Code Playgroud)
build.xml中的相关部分如下所示:
<taskdef name="mytask" classname="dec102012.MyAntTask" classpath="dec102012"/>
<target name="main">
<mytask message="Hello World! MyVeryOwnTask works!"/>
</target>
Run Code Online (Sandbox Code Playgroud) 我希望使用VLC作为我在C#中编写的应用程序中的视频播放器的基础(因为VLC是能够正确解码我正在使用的格式的少数播放器之一),但是我需要它的主要任务表演是:
现在我通过VLC扩展LUA脚本执行此操作,但VLC通过vlc.var.get返回的时间(输入,"时间")缺乏精度,并且可能因同一视频文件中的同一帧而异文件读数.帧浏览也不太可靠.另外,以这种方式接收的值不能自动转发到另一个应用程序.
我尝试使用libVLC的LibVLC.NET包装器,但我仍然无法在帧之间浏览,并且返回的毫秒计数值相当奇怪.这几乎就像VLC(libVLC)在回放期间没有返回实际时间值,而是某种舍入值,当读取精度低至毫秒时,该值有很大的延迟.标准VLC接口既不能显示精确的时间值,也不能显示毫秒,也不能在帧之间浏览.
有没有可行的方法通过以某种方式从使用.NET编写的应用程序运行VLC来执行VLC的上述两个任务?或者我应该考虑其他选择?
我从线程X中的对象一个接一个地发出两个信号A和B,并且两个连接的槽位于主线程中.连接是QueuedConnection(由于多线程连接).我的问题是:在调用插槽时信号的顺序是否受到尊重,或者它们是否有可能以任意顺序执行?
通常,价值观是积极的.例如,TCP/UDP序列号始终为正值.两者int并unsigned int有足够大的存储即使是最大的sequence number,所以我可以使用这些类型.当已知值为正值时,还有许多其他示例.
unsigned当常规signed类型的容量足够(并且通常绰绰有余)时,是否有任何理由使用类型?
我个人倾向于使用常规类型,因为:
int可能比uint或更具可读性unsigned intUINT等.使用unsignedI型的原因可以想象:
今天我偶然发现了一个像这样的代码片段:
class A
{
A() = default;
A (const A&) = delete;
...
}
Run Code Online (Sandbox Code Playgroud)
我从未见过delete或default关键字.它们是C++ 11标准的一部分吗?它们用于什么?
我一直在研究C++ 11的一些新功能,并且对其中的一些非常印象深刻,特别是用户定义的文字.
这些允许您定义表单的文字,999_something其中something控制999对生成文字所做的操作.所以不必再使用:
#define MEG * 1024 * 1024
int ten_meg = 10 M;
Run Code Online (Sandbox Code Playgroud)
我认为大量实现下划线会很好,比如1_000_000_blahPerl的可读性,虽然Perl在某种程度上可读的想法对我来说似乎很幽默:-)
它对于像1101_1110_b和的二进制值也很方便0011_0011_1100_1111_b.
显然,由于_字符,这些将需要是原始模式类型,处理C字符串,我没关系.
我无法弄清楚的是如何根据操作数大小提供不同的类型.例如:
1101_1110_b
Run Code Online (Sandbox Code Playgroud)
应该给出一个char(假设char当然是8位),而:
0011_0011_1100_1111_b
Run Code Online (Sandbox Code Playgroud)
将提供16位类型.
我可以从文字运算符函数operator""本身(通过计数数字字符)获取操作数的长度,但返回类型似乎固定到函数,所以我不能基于此返回不同的类型.
这可以用一个单一的后缀来完成_b用户定义类型框架内,还是需要求助于除了手动拆分类型(_b8,_b16等等),并提供主要功能重复?
c++ templates user-defined-literals variadic-templates c++11
我专注std::common_type于我的类型.我定义了以下专业:
common_type<my_type, my_type>
Run Code Online (Sandbox Code Playgroud)
一切都很好.然后有人来了并打电话std::common_type<my_type, my_type &>.如果传递引用而不是引用(因为它调用std::decay类型),则默认版本的行为相同.但是,它并不遵循std::common_type我需要正确工作的非参考版本.有没有比这样做更好的方法(为了简单起见,省略了对const的rvalue-reference):
common_type<my_type, my_type>
common_type<my_type, my_type &>
common_type<my_type, my_type const &>
common_type<my_type, my_type volatile &>
common_type<my_type, my_type const volatile &>
common_type<my_type, my_type &&>
common_type<my_type, my_type volatile &&>
common_type<my_type &, my_type>
common_type<my_type const &, my_type>
common_type<my_type volatile &, my_type>
common_type<my_type const volatile &, my_type>
common_type<my_type &&, my_type>
common_type<my_type volatile &&, my_type>
common_type<my_type &, my_type &>
common_type<my_type &, my_type const &>
common_type<my_type &, my_type volatile &>
...
Run Code Online (Sandbox Code Playgroud)
当然有更好的方法吗?根据我的统计,如果我们忽略const && …
c++ templates template-specialization template-meta-programming c++11
我正在编写一个包含许多函数对象的库,这些函数对象的类具有多个operator()重载,这些重载不依赖于类的状态而不会改变它.现在,我试图让我的代码使用许多旧式API(它不是随机需要,我实际上不得不处理这样的API),因此决定使函数对象可以转换为任何一个对应的函数指针重载.在某些时候,我意识到我有太多这样的转换来运行指针运算符,我理论上应该能够编写一个可变转换运算符.这是一个实现这种可变参数运算符的类:
struct foobar
{
template<typename... Args>
using fptr_t = void(*)(Args... args);
template<typename... Args>
operator fptr_t<Args...>() const
{
return [](Args... args) {
// Whatever
};
}
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用lambda转换函数指针来实现转换运算符,这不是问题,因为我拥有的每个函数对象都是无状态的.目标是能够使用如下类:
int main()
{
void(*foo)(int) = foobar();
void(*bar)(float, double) = foobar();
}
Run Code Online (Sandbox Code Playgroud)
g ++使用预期的语义编译此代码没有问题.但是,clang ++ 拒绝它时出现模板替换失败错误:
Run Code Online (Sandbox Code Playgroud)main.cpp:21:11: error: no viable conversion from 'foobar' to 'void (*)(int)' void(*foo)(int) = foobar(); ^ ~~~~~~~~ main.cpp:11:5: note: candidate function [with Args = int] operator fptr_t<Args...>() const ^ 1 error generated.
请注意,只要不涉及可变参数模板,clang ++对此类转换运算符没有任何问题.如果我使用单个模板参数,编译代码就没有问题.现在,编译器是接受还是拒绝上面的代码?
c++ function-pointers language-lawyer variadic-templates c++14
我有这样的类型:
template<typename T>
struct wrapper
{
using foo = typename T::foo;
using bar = typename T::bar;
using baz = typename T::baz;
// More of those...
};
Run Code Online (Sandbox Code Playgroud)
我想foo,bar,baz和当且仅当在同等类型的存在来定义同等类型的别名T.使用std::conditionalallow的解决方案当它不存在时用其他东西替换它,但我不知道如果模板类型中不存在相应的类型,它根本不存在.wrapper<T>如果T没有定义其中一个类型别名,则上面的代码在实例化时会导致错误.
我不能wrapper继承,T因为wrapper不应该做所有事情都T可以.此外,使用部分专业化将导致某种指数爆炸,并很快变得不可维护.我大概可以做foo,bar...模板类型别名注入一个std::enable_if在默认模板参数,但随后用户会写wrapper<T>::foo<>,wrapper<T>::bar<>而不是wrapper<T>::foo,wrapper<T>::bar等...我不希望出现这种情况.
是否有一种简单但可维护的方法来定义这样的类型别名只有在相应的类型别名存在时T?