Tag: 模板

C ++ 11模板别名作为模板模板参数导致不同的types?

我们在编译以下源代码时观察到一个奇怪的行为: template<template<class> class TT> struct X { }; template<class> struct Y { }; template<class T> using Z = Y<T>; int main() { X<Y> y; X<Z> z; z = y; // it fails here } 这是一个从模板别名的c ++ 11标准提案中稍作修改的例子: http : //www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf (请参阅第4页)还要注意,提案“声明y和z是同一types”。 因此,在我们的解释中,应该可以从y分配(或复制构造)z。 但是,这个代码不能用gcc 4.8.1和clang 3.3编译。 这是编译器中的错误还是我们误解了标准? 在此先感谢,craffael等;) PS Clang错误信息是: error: no viable overloaded '=' note: […]

为什么标准中不允许初始化模板参数列表中的常量相关types?

在对这篇文章的回答“ (部分地)专门化非依赖types的非types模板参数 ”中,它指出: 对应于专门的非types参数的模板参数的types不应该依赖于专门化的参数。 [例如: template <class T, T t> struct C {}; template <class T> struct C<T, 1>; // error template< int X, int (*array_ptr)[X] > class A {}; int array[5]; template< int X > class A<X,&array> { }; // error – 例子] 我的问题是为什么这个限制在这里? 至less有一个用例,我发现这个限制干扰了编写干净的代码。 例如 template <typename T, T*> struct test; template <typename […]

为什么这个从属名称查找find一个全局标识符而不是方法?

当编译器试图parsingi.template hi<T>(); 它发现hi在全球命名空间,而不是在i ( ideone )的方法hi 。 为什么? #include <cstdio> // Define 'hi' and 'bye' in the global namespace; these should *not* be used template<typename T> struct hi { }; template<typename T> struct bye { }; // Foo needs to be templated for Foo::Inner to be a dependent type (I think) template<typename T> struct Foo […]

C ++模板和内联

当我正在编写一个简单的(非模板)类时,如果函数实现是“适当的”,它会被自动视为inline 。 class A { void InlinedFunction() { int a = 0; } // ^^^^ the same as 'inline void InlinedFunction' } 谈论基于模板的类时,这条规则是什么? template <typename T> class B { void DontKnowFunction() { T a = 0; } // Will this function be treated as inline when the compiler // instantiates the template? }; 另外, inline规则如何应用于非嵌套模板函数,如 […]

我可以使用模板别名作为模板模板参数吗?

我可以使用模板别名作为模板模板参数吗? template <template <typename…> class> struct foo {}; template <typename T> using simple_ptr = std::unique_ptr<T>; foo<std::unique_ptr> a; // this doesn't work, std::unique_ptr has two parameters foo<simple_ptr> b; // does this work?

g ++和clang ++与积分模板参数的不同行为

我有以下C ++ 11代码。 #include <type_traits> using IntType = unsigned long long; template <IntType N> struct Int {}; template <class T> struct is_int : std::false_type {}; template <long long N> struct is_int<Int<N>> : std::true_type {}; int main() { static_assert (is_int<Int<0>>::value, ""); return 0; } Clang ++ 3.3编译代码,但在g ++ 4.8.2静态断言失败 $ g++ -std=c++11 main.cpp main.cpp: In function […]

本地types作为C ++中的模板参数

这是我的代码 #include <vector> template <typename T, template<typename> class C = std::vector > struct FooBar { /*codez*/ }; template<typename T> struct Global{}; int main() { struct Local{}; FooBar<Local,Global> k; } 这是我得到的错误 template argument for 'template<class T, template<class> class C> struct FooBar' uses local type 'main()::Local' 标准的哪一部分说这是错误的? 我正在使用gcc 4.5.1。 如何使这个代码工作?

ES6模板文字比string连接更快吗?

有没有人做基准? 我很好奇,如果HTML生成代码更快与string连接或与节点和现代浏览器中的模板文字。 例如: string连接 "<body>"+ "<article>"+ "<time datetime='" + date.toISOString() +"'>"+ date +"</time>"+ "</article>"+ "</body>" 模板文字 `<body> <article> <time datetime='${ date.toISOString() }'>${ date }</time> </article> </body>`

为什么不能重载类模板?

读这个问题让我怀疑:是否有一个技术原因不允许类模板重载? 通过重载,我的意思是有几个相同名称的模板,但不同的参数,例如 template <typename T> struct Foo {}; template <typename T1, typename T2> struct Foo {}; template <unsigned int N> struct Foo {}; 编译器pipe理处理重载的函数和函数模板,是不是可以应用类模板相同的技术(例如名称修改)? 起初,我认为也许这会在单独使用模板标识符时造成一些模糊性问题,但唯一可能发生的情况是将其作为模板模板parameter passing,因此可以使用参数的types来select适当的超载: template <template <typename> class T> void A {}; template <template <unsigned int> class T> void B {}; A<Foo> a; // resolves to Foo<T> B<Foo> b; // resolves to Foo<N> […]

select正确的UI模板工具 – dust.js?

我在一个基于Java的大型Web应用程序上工作,这个应用程序在过去5年左右的时间里已经build立起来了 – 用户界面需要重新整理/重新编写。 我们正在研究可用的用户界面工具/库/框架,并已经遇到了dust.js作为模板选项。 问题:我有兴趣听到dust.js的用户在想什么: 它成功了吗? 这个容易用吗? 有足够的文件logging吗? 社区支持好吗? ( ST标签上的“dust.js”只有6个问题!) 与其他模板工具(如Underscore的模板, Googleclosures模板 , 把手和小胡子)相比,有什么优点和缺点。 使用MV *结构框架,例如Backbone.js ( 在线书籍 )有没有什么问题? 一些背景: 为什么我们对dust.js感兴趣:以下的LinkedIn博客文章首先引起了我们的关注: 把JSP留在尘土中:将LinkedIn移动到dust.js客户端模板 客户端模板化:胡子,把手,dust.js等等 这两篇文章中的第二篇非常好地回答了问题5,但是来自LinkedIn的观点很less有谷歌的结果详细说明了模板系统,或者暗示它是一个stream行的select。 另外,这个post提到他们已经扩展了function,并希望有一天能够为原来的项目做出贡献。 我担心,直到他们这样做,我们可能还需要扩展function。 说到这一点,LinkedIn对模板系统的原始要求与我们非常接近(见下文),他们在select之前已经做了一些非常彻底的调查。 我们的要求: DRY :如果我们selectLinkedIn的完整方法,我们理想的是希望在服务器(基于Java)和客户端使用模板系统,或者只使用客户端模板系统; Instead of using a JSP, GSP, or ERB to assemble a page server side and send back HTML, we have the server send back […]