基于范围的实现以g ++编译,但不是在clang ++中编译

时间:2012-02-28 02:20:41

标签: c++ for-loop c++11 clang

以下基于范围的for-snippet在g ++ 4.6.1中编译得很好,但在oneiric amd64上没有使用clang ++ 3.1版(trunk 151577)。

一个非常基本的基于范围的(例如std :: vector)似乎工作正常,所以我的枚举实现有问题。

我正在寻找理智检查或解决方法。

如果某人有基于范围的解决办法 - 因为clang ++很满意,那就太棒了。

template< typename E >
class Enum
{
public:
    Enum() : m_e( E::Last ) { }
    Enum( E t ) : m_e( t ) { }
    E operator()() const
    {
        return m_e;
    }

public: 
    class Iterator
    {
    public:
        Iterator( int val ) : m_val( val )    { }
        E operator*( void ) const
        {
            return (E) m_val;
        }
        void operator++( void )
        {
            ++m_val;
        }
        bool operator!=( Iterator rhs ) const
        {
            return m_val != rhs.m_val;
        }
    private:
        int  m_val;
    };
private:
    E m_e;
};

enum class eCOLORS
{
    kBLUE=0, kGREEN, kRED, kPURPLE,
    First=kBLUE, Last=kPURPLE
};

Enum<eCOLORS>::Iterator begin(const Enum<eCOLORS>& b)
{
    return Enum<eCOLORS>::Iterator( (int)(eCOLORS::First ));
}

Enum<eCOLORS>::Iterator end(const Enum<eCOLORS>& b)
{
    return Enum<eCOLORS>::Iterator( (int)(eCOLORS::Last ));
}

int main()
{
    Enum<eCOLORS> e; 
    // for( const auto x : Enum<eCOLORS>() )
    for( auto it=begin(e); 
        it!=end(e); ++it )
    {

    }
}

错误列在下面

clang++ -g -std=c++0x \
    sandbox.cpp -o sandbox

clang: /mnt/home/foobar/src/llvm/tools/clang/lib/AST/Decl.cpp:1001: bool clang::NamedDecl::isCXXInstanceMember() const: Assertion `isCXXClassMember() && "checking whether non-member is instance member"' failed.
0  clang           0x0000000001aecc4f
1  clang           0x0000000001aed179
2  libpthread.so.0 0x00002aeeaeda1060
3  libc.so.6       0x00002aeeaf98b3a5 gsignal + 53
4  libc.so.6       0x00002aeeaf98eb0b abort + 379
5  libc.so.6       0x00002aeeaf983d4d __assert_fail + 221
6  clang           0x0000000000ed8f9e clang::NamedDecl::isCXXInstanceMember() const + 174
7  clang           0x0000000000a51095 clang::Sema::CheckQualifiedMemberReference(clang::Expr*, clang::QualType, clang::CXXScopeSpec const&, clang::LookupResult const&) + 213
8  clang           0x0000000000a55520 clang::Sema::BuildMemberReferenceExpr(clang::Expr*, clang::QualType, clang::SourceLocation, bool, clang::CXXScopeSpec const&, clang::SourceLocation, clang::NamedDecl*, clang::LookupResult&, clang::TemplateArgumentListInfo const*, bool) + 672
9  clang           0x0000000000a518d4 clang::Sema::BuildMemberReferenceExpr(clang::Expr*, clang::QualType, clang::SourceLocation, bool, clang::CXXScopeSpec&, clang::SourceLocation, clang::NamedDecl*, clang::DeclarationNameInfo const&, clang::TemplateArgumentListInfo const*) + 772
10 clang           0x0000000000b50378
11 clang           0x0000000000b58a19
12 clang           0x0000000000b56362
13 clang           0x0000000000b4e87b clang::Sema::SubstExpr(clang::Expr*, clang::MultiLevelTemplateArgumentList const&) + 75
14 clang           0x0000000000b64a69 clang::Sema::SubstInitializer(clang::Expr*, clang::MultiLevelTemplateArgumentList const&, bool) + 169
15 clang           0x0000000000b6d57f clang::Sema::InstantiateMemInitializers(clang::CXXConstructorDecl*, clang::CXXConstructorDecl const*, clang::MultiLevelTemplateArgumentList const&) + 1135
16 clang           0x0000000000b6cb13 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool) + 2307
17 clang           0x0000000000b6dadd clang::Sema::PerformPendingInstantiations(bool) + 461
18 clang           0x00000000008e0bb3 clang::Sema::ActOnEndOfTranslationUnit() + 419
19 clang           0x00000000008757d0 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) + 112
20 clang           0x00000000008727c6 clang::ParseAST(clang::Sema&, bool) + 326
21 clang           0x000000000075623e clang::CodeGenAction::ExecuteAction() + 958
22 clang           0x0000000000612f3d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 957
23 clang           0x00000000005fb55a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 3098
24 clang           0x00000000005f17ce cc1_main(char const**, char const**, char const*, void*) + 5918
25 clang           0x00000000005f7679 main + 729
26 libc.so.6       0x00002aeeaf97630d __libc_start_main + 237
27 clang           0x00000000005effe9
Stack dump:
0.  Program arguments: /usr/local/bin/clang -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name sandbox.cpp -mrelocation-model static -mdisable-fp-elim -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -g -resource-dir /usr/local/bin/../lib/clang/3.1 -I /home/kfeng/dev/pitbull/cpp/inc/ -I /home/kfeng/src/stlsoft/include/ -I /home/kfeng/src/gtest/include/ -I /home/kfeng/src/gmock/include/ -I /home/kfeng/src/pantheios/include/ -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/x86_64-linux-gnu -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/backward -internal-isystem /usr/local/include -internal-isystem /usr/local/bin/../lib/clang/3.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++0x -fdeprecated-macro -fdebug-compilation-dir /home/kfeng/dev/pitbull/cpp/sbx -ferror-limit 19 -fmessage-length 181 -mstackrealign -fgnu-runtime -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-fragile-abi -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/sandbox-0nnyL0.o -x c++ sandbox.cpp 
1.  <eof> parser at end of file
2.  sandbox.cpp:5:2: instantiating function definition 'Enum'
clang: error: unable to execute command: Aborted
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang: note: diagnostic msg: Please submit a bug report to http://llvm.org/bugs/ and include command line arguments and all diagnostic information.
clang: note: diagnostic msg: Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/sandbox-Fp11tf.ii
clang: note: diagnostic msg: /tmp/sandbox-Fp11tf.sh

1 个答案:

答案 0 :(得分:2)

这为我编译了稳定的clang释放。看起来这是后备箱中的一个错误。

% clang --version
clang version 3.0 (tags/RELEASE_30/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
% clang++ -o test test.cc -std=c++0x
%