我很难理解正式排序规则的工作原理,如C++模板的第12章,D.Vandevoorde和NM Josuttis的完整指南所述.在本书的第188页,作者给出了以下场景,用于确定两个可行的函数模板中哪一个更加专业化:
从这两个模板中,我们通过替换前面描述的模板参数来合成两个参数类型列表:
(A1)和(A2*)(其中A1和A2是唯一组成的类型).显然,通过替换A2*,对第二个参数类型列表推导出第一个模板是成功的T.但是,没有办法使T*第二个模板与A1第一个列表中的非指针类型匹配.因此,我们正式得出结论,第二个模板比第一个模板更专业.
我想要理解这个例子.
编辑
我相信上面引用的两个功能模板都是
template<typename T>
int f(T)
{
return 1;
}
template<typename T>
int f(T*)
{
return 2;
}
Run Code Online (Sandbox Code Playgroud) 有人会推荐一个命令行实用程序,它可以播放以毫秒为单位指定的音频文件的任何部分,例如
player -start-time=0.1234 end-time=5.6789 audio.wav
Run Code Online (Sandbox Code Playgroud)
我遇到过的音频播放器似乎都没有这个功能.vlc支持开始和结束时间,但仅限秒,而Audacity似乎没有太多的命令行选项.
它花了整整一年但我已经通过C++模板 - David Vandevoorde和Nicolai M. Josuttis 的完整指南,现在我被困在最后一章的最后一部分 - 值粘合剂,过去3年周.
值粘合剂的介绍,第457页的第22.8节,很容易理解,但我发现本节其余部分的例子很难理解.具体地讲,我不明白是什么了Binder(见函子/ binder1.hpp和函子/ binder2.hpp和BinderParams类(见函子/ binderparams.hpp)正在努力实现的.我的难度不与模板语法,但背后的意图,这些类.
这个Binder类如何执行绑定?以及如何BinderParams类标识(?)的参数?这就是我难以看到/理解的东西.
我想从批处理脚本中读取/返回单个字符,而不必像getChar()在C/C++中那样点击回车键.我该怎么做?
是否可以使用递归宏来编写所需数量的函数参数.例如:
void foo( const char (&row1)[3] , const char (&row2)[3] , const char (&row3)[3] )
{
}
void foo( const char (&row1)[3] , const char (&row2)[3] , const char (&row3)[3] , const char (&row4)[3] , const char (&row5)[3] , const char (&row6)[3] )
{
}
Run Code Online (Sandbox Code Playgroud)
我知道引入了可变参数模板来解决这样的问题,但我仅限于C++ 98 ATM.
以下MWE在gcc 4.8.2上编译,但在MSVC 2008(公司政策)上没有编译
struct B
{
};
struct A
{
typedef B Handler;
};
template<typename T>
struct Foo
{
typedef typename T::Handler Type;
};
template<typename T>
struct Bar
{
friend struct Foo<T>::Type; // MSVC 2008 does not like this
typedef typename Foo<T>::Type Foo;
};
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
MSVC 2008错误
error C2649: 'Foo<T>::Type' : is not a 'struct'
Run Code Online (Sandbox Code Playgroud)
这是编译器错误还是我在这里做了非法的事情?更重要的是有修复吗?
为什么以下尝试重载构造函数Foo::Foo失败?此外,我很欣赏替代方案/解决方法
#include <vector>
#include <type_traits>
namespace xyz
{
struct MemoryManager{};
template<typename T , typename Alloc=MemoryManager>
class vector
{
};
}
template<typename T, template<typename,typename> class V , typename A>
struct Foo
{
template<typename U = T ,
typename Dummy = typename std::enable_if<
std::is_same<std::vector<U>, V<U,A> >::value >::type >
Foo() // when instantiated with the std vector
{
}
template<typename U = T ,
typename Dummy = typename std::enable_if<
std::is_same<xyz::vector<U>, V<U,A> >::value >::type >
Foo() // when instantiated with …Run Code Online (Sandbox Code Playgroud) 我无法解释为什么在示例代码中调用new Foo [4]自定义operator new[]请求68字节 - 比它应该多4个字节(sizeof(Foo) == 16),而更神秘的调用Foo::operator new[]( 4 * sizeof( Foo ) )正确地请求64字节.请注意,当std::vector<long> m_dummy从Foo两个调用中删除成员时,请求16个字节(ideone上的代码).
#include <vector>
#include <iostream>
struct MemoryManager
{
static void* allocate( unsigned size )
{
static char block[256];
return block;
}
};
class Foo
{
public:
void* operator new[]( size_t size )
{
std::cout << "operator new[] : data size -- " << size << std::endl;
return MemoryManager::allocate( size );
} …Run Code Online (Sandbox Code Playgroud) 是否可以T从其指向memmber的指针推断出类的类型,T::*f如下所示.
struct Foo
{
void func(){}
};
template<typename T, void (T::*f)()>
void bar()
{
}
int main()
{
bar<Foo,Foo::func>();
// bar<Foo::func>(); // Desired
}
Run Code Online (Sandbox Code Playgroud) c++ templates pointer-to-member type-deduction template-argument-deduction
我在GCC上编译以下MWE时遇到上述错误
#include <string>
void frobnigate( const std::string& str )
{
std::string::const_iterator& iter = str.begin();
}
int main()
{
frobnigate( "all things!!!" );
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么还是GCC问题?
c++ ×8
templates ×5
c++11 ×2
audio ×1
audio-player ×1
batch-file ×1
command-line ×1
dos ×1
friend ×1
gcc ×1
generics ×1
macros ×1
new-operator ×1
recursion ×1
sfinae ×1
template-argument-deduction ×1
visual-c++ ×1