jjzjj

c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明

coder 2024-02-12 原文

我在嵌套命名空间中有一个模板类的前向声明

namespace n1
{
    namespace n2
    {
        template <typename T, typename S>
        struct A;
    }
    using n2::A;
}

接着是一个定义,实际上它在不同的文件中,中间有一些东西:

struct X { };

namespace n1
{
    namespace n2
    {
        template <typename T, typename S = X>
        struct A { };
    }
    using n2::A;
}

那么以下总是可以的:

n1::n2::A <int> a;

但是这个捷径

n1::A <int> a;

在 clang 中给出编译错误

error: too few template arguments for class template 'A'

除非我删除前向声明; g++ 两者都接受。

clang 似乎保留在第一个不包含默认模板参数的声明中(我不能包含它,因为我还没有定义 X)。

如果我使用单个命名空间没有问题(但这不是解决方案)。

我做错了什么,或者哪个编译器是正确的?快捷方式如何与前向声明和嵌套命名空间一起使用?我需要所有这些。

前向声明 X + S 的默认参数当然可以,但是太乏味了(实际上有几十个,整个文件结构会改变)。

最佳答案

template<class T> using myname = some::templatething<T>;

然后你可以使用我的名字

在你的情况下贴一个template<class T,class S=X> using A = n2::A<T,S>;在你的n1

刚刚写了一个与此相关的答案的精华 Symbol not found when using template defined in a library顺便说一句,读一读。

好吧,它没有被勾选,所以我会再帮助一些!

不会编译

#include <iostream>

namespace n1 {
namespace n2 {
template<class U,class V> struct A;
}
template<class U,class V> using A = n2::A<U,V>;
}

static n1::A<int,int>* test;

struct X {};
namespace n1 {
namespace n2 {
template<class U,class V> struct A {};
}
template<class U,class V=X> using A = n2::A<U,V>;
}

static n1::A<int> test2;


int main(int,char**) {

    return 0;
}

为什么? C++的“先声明规则”

这是编译器的输出:

make all 
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -Isrc -c src/main.cpp -o build/main.o
src/main.cpp:26:17: error: wrong number of template arguments (1, should be 2)
 static n1::A<int> test2;
                 ^
src/main.cpp:13:47: error: provided for ‘template<class U, class V> using A = n1::n2::A<U, V>’
 template<class U,class V> using A = n2::A<U,V>;
                                               ^
src/main.cpp:26:24: error: invalid type in declaration before ‘;’ token
 static n1::A<int> test2;
                        ^
src/main.cpp:16:24: warning: ‘test’ defined but not used [-Wunused-variable]
 static n1::A<int,int>* test;
                        ^
src/main.cpp:26:19: warning: ‘test2’ defined but not used [-Wunused-variable]
 static n1::A<int> test2;
                   ^
make: *** [build/main.o] Error 

好吧,它提示未使用的变量,很公平,但请注意它是对第 13 行的引用,那是因为它使用了第一个定义,默认模板参数非常原始,我现在不能向您引用规范。 https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fdefault_args_for_templ_params.htm

虽然这可能会提供一些见解。

无论如何请注意:

这个编译

#include <iostream>

namespace n1 {
namespace n2 {
template<class U,class V> struct A;
}
template<class U,class V> using A = n2::A<U,V>;
}

static n1::A<int,int>* test;

struct X {};
namespace n1 {
namespace n2 {
template<class U,class V> struct A {};
}
template<class U,class V=X> using B = n2::A<U,V>;
}

static n1::B<int> test2;


int main(int,char**) {

    return 0;
}

因为B没有事先定义。

构建输出

make all 
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -Isrc -c src/main.cpp -o build/main.o
src/main.cpp:16:24: warning: ‘test’ defined but not used [-Wunused-variable]
 static n1::A<int,int>* test;
                        ^
src/main.cpp:26:19: warning: ‘test2’ defined but not used [-Wunused-variable]
 static n1::B<int> test2;
                   ^
g++  build/main.o  -o a.out

看,很好:)

请记住,对于前向声明,您只能使用 *s 和 &s(因为它们的大小已知,实际上是固定大小)- oh 和 &&s

所以您需要立即获取默认值!

关于c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18543182/

有关c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  5. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  6. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  7. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  8. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  9. ruby-on-rails - 使用回形针的嵌套形式 - 2

    我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?

  10. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

随机推荐