草庐IT

c++ - 模板内的模板 VS 2005 问题?

coder 2024-02-26 原文

下面的代码(用 clang 和 gcc 编译得很好)。问题是这段代码违反了 C++03 标准, 或者这是 VS 2005 错误?如果这是错误,是否有解决方法?

更新:我通过使用前向声明找到了解决方法:

//forward declaration
template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
struct FilteredConstructor;

//implementation
template<typename T>
class FilteredConstructor<T, true> {/*code here*/};

但是根据标准仍然存在关于此类代码有效或无效的问题

namespace {
    struct CoreTypesFilter {
        template<typename T> struct Acceptor {
           static const bool IsAccepted = false;
        };
    };
}


template<class Filter>
class QVariantConstructor {
    template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
    struct FilteredConstructor {
       FilteredConstructor(const QVariantConstructor &tc) {}
    };

    template<typename T>
    struct FilteredConstructor<T, /* IsAcceptedType = */ false> {
        FilteredConstructor(const QVariantConstructor &tc) {}
    };
public:
    template<typename T>
    void delegate(const T*)
    {
        FilteredConstructor<T> tmp(*this);
    }
};
//comment or uncomment them to build on VS or linux
#define _TCHAR char
#define _tmain main

int _tmain(int argc, _TCHAR* argv[])
{
    QVariantConstructor<CoreTypesFilter> vc;
    vc.delegate("test");//this line trigger compile error
    return 0;
}

VS 2005 编译器的编译错误:

error C2976: 'QVariantConstructor::FilteredConstructor' : too few template arguments

1>        with
1>        [
1>            Filter=`anonymous-namespace'::CoreTypesFilter
1>        ]
1>        see declaration of 'QVariantConstructor::FilteredConstructor'
1>        with
1>        [
1>            Filter=`anonymous-namespace'::CoreTypesFilter
1>        ]
1>        see reference to function template instantiation 'void QVariantConstructor::delegate(const T *)' being compiled
1>        with
1>        [
1>            Filter=`anonymous-namespace'::CoreTypesFilter,
1>            T=char
1>        ]
1>        error C2514: 'QVariantConstructor::FilteredConstructor' : class has no constructors
1>        with
1>        [
1>            Filter=`anonymous-namespace'::CoreTypesFilter
1>        ]
1>       see declaration of 'QVariantConstructor::FilteredConstructor'
1>        with
1>        [
1>            Filter=`anonymous-namespace'::CoreTypesFilter
1>        ]

最佳答案

我快速阅读了标准的相关部分,问题似乎归结为您如何阅读 ISO/IEC 14882:2003 第 14.1 节第 9 段

默认模板参数是 模板参数= 之后指定的 模板参数 (14.3)。可以为任何类型的template-parameter(类型、非类型、模板)指定默认的template-argument。默认的 template-argument 可以在类模板声明或类模板定义中指定。默认的template-argument 不应在函数模板声明或函数模板定义中指定也不在类模板成员定义的template-parameter-list。不应在友元模板声明中指定默认的 template-argument

严格来说,这似乎暗示您的代码是非法的。然而,大多数实现似乎将其解释为 已声明模板成员的定义

健康警告,这是基于一读。我的回答很可能是错误的或不完整的,请指正。

关于c++ - 模板内的模板 VS 2005 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18534654/

有关c++ - 模板内的模板 VS 2005 问题?的更多相关文章

  1. ruby-on-rails - Railstutorial : db:populate vs. 工厂女孩 - 2

    在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

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

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

  5. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  6. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  7. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  9. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  10. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

随机推荐