下面的代码正确检查类型T是否有方法sort.但是当我修改标记(*)为更改decltype(&U::sort,...)为的行decltype(U::sort,...)(符号&被删除)时,代码始终返回false.
为什么?
为什么这个名字本身还不够?这&是什么意思?
#include <iostream>
#include <type_traits>
template <typename T>
class has_sort {
template <typename U>
static auto check(bool) -> decltype(&U::sort, std::true_type()); // (*)
template <typename U>
static std::false_type check(...);
public:
using type = decltype(check<T>(true));
static bool const value = type::value;
};
int main() {
struct Foo { void sort(); };
struct Foo2 { void sort2(); };
std::cout << "Foo: " << has_sort<Foo>::value << …Run Code Online (Sandbox Code Playgroud) 我想制作一个网站的静态副本,保留现有的URL.问题是URL看起来像:
HTTP://mysite/index.php ID = XXX
和Apache不想找到文件"index.php?id = XXX".相反,它将请求解释为具有查询"id = XXX"的文件"index.php".
我怎么能要求Apache停止处理问号?
最后,我的解决方案:
1)将文件从"index.php?id = XXX"重命名为"index.php_id = XXX"
2)添加到.htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ $1_%{QUERY_STRING} [L]
Run Code Online (Sandbox Code Playgroud) Stroustrup的书提供了一个如何回答这个问题的例子:" f(x)如果x是类型的话,是否可以调用X"(第28.4.4节"使用Enable_if的更多示例").我试图重现这个例子,但是出了点问题,无法理解.
在我的下面的代码中,有一个函数f(int).我希望那时的结果has_f<int>::value是1(true).实际结果是0(false).
#include <type_traits>
#include <iostream>
//
// Meta if/then/else specialization
//
struct substitution_failure { };
template<typename T>
struct substitution_succeeded : std::true_type { };
template<>
struct substitution_succeeded<substitution_failure> : std::false_type { };
//
// sfinae to derive the specialization
//
template<typename T>
struct get_f_result {
private:
template<typename X>
static auto check(X const& x) -> decltype(f(x));
static substitution_failure check(...);
public:
using type = decltype(check(std::declval<T>())); …Run Code Online (Sandbox Code Playgroud) 使用 Mac OS X API,我试图保存一个应用了 Quartz 过滤器的 PDF 文件,就像可以从预览应用程序的“另存为”对话框中一样。到目前为止,我已经编写了以下代码(使用 Python 和 pyObjC,但这对我来说并不重要):
-- filter-pdf.py: 开始
from Foundation import *
from Quartz import *
import objc
page_rect = CGRectMake (0, 0, 612, 792)
fdict = NSDictionary.dictionaryWithContentsOfFile_("/System/Library/Filters/Blue
\ Tone.qfilter")
in_pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename ("test
.pdf"))
url = CFURLCreateWithFileSystemPath(None, "test_out.pdf", kCFURLPOSIXPathStyle,
False)
c = CGPDFContextCreateWithURL(url, page_rect, fdict)
np = CGPDFDocumentGetNumberOfPages(in_pdf)
for ip in range (1, np+1):
page = CGPDFDocumentGetPage(in_pdf, ip)
r = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
CGContextBeginPage(c, r)
CGContextDrawPDFPage(c, page)
CGContextEndPage(c)
Run Code Online (Sandbox Code Playgroud)
-- filter-pdf.py: 结束
不幸的是,没有应用过滤器“蓝色色调”,输出 PDF …