小编Tom*_*asu的帖子

函数专门化基于if模板参数是shared_ptr

我正在尝试检测类型是否为a shared_ptr<T>,如果是,则调度到特定的函数模板或覆盖.

这是我实际尝试的简化版本:

#include <type_traits>
#include <memory>
#include <cstdio>

template <class T> struct is_shared_ptr : std::false_type {};
template <class T> struct is_shared_ptr<std::shared_ptr<T> > : std::true_type {};

class Foo { };
typedef std::shared_ptr<Foo> SharedFoo;

template<class T> void getValue();

template<class T, typename std::enable_if<is_shared_ptr<T>::value>::type = 0>
void getValue()
{
    printf("shared!\n");
}

template<class T, typename std::enable_if<!is_shared_ptr<T>::value>::type = 0>
void getValue()
{
    printf("not shared!\n");
}

int main(int, char **)
{
    getValue<SharedFoo>();
    getValue<Foo>();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它编译得很好,但似乎实际的函数从未实际生成,因为代码没有链接以下错误:

/tmp/ccjAKSBE.o: In function `main':
shared_test.cpp:(.text+0x10): undefined reference to …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

4
推荐指数
1
解决办法
3543
查看次数

无法使用llvm和clang解析C++

我正在用llvm编写一个小工具来解析C和C++代码,但我似乎无法让它成功地解析C++.我可能错过了一些明显的东西.

这是我到目前为止:

#include <iostream>

#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"

#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Frontend/LangStandard.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/DirectoryLookup.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Parse/ParseAST.h"

class MyASTConsumer : public clang::ASTConsumer {
    public:
        bool HandleTopLevelDecl(clang::DeclGroupRef d);
        virtual ~MyASTConsumer() { }
};

bool MyASTConsumer::HandleTopLevelDecl(clang::DeclGroupRef d)
{
    for(auto ii = d.begin(); ii != d.end(); ii++)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ llvm clang

3
推荐指数
1
解决办法
573
查看次数

标签 统计

c++ ×2

c++11 ×1

clang ×1

llvm ×1

templates ×1