简体   繁体   English

用clang编译MSVC std lib。 在类范围内显式模板函数专门化

[英]Compiling MSVC std lib with clang. Explicit template function specialization at class scope

I'm trying to compile the MSVC standard library using Clang. 我正在尝试使用Clang编译MSVC标准库。 But it fails because the standard library uses explicit template function specialization at class scope. 但这失败了,因为标准库在类范围内使用显式模板函数专门化。

This is a MS extension, and apparently not available in Clang. 这是MS扩展名,显然在Clang中不可用。

Here's a simple example that compiles fine with MSVS, but not with Clang. 这是一个简单的示例,可以使用MSVS进行编译,但不能使用Clang进行编译。

template<class T>
class A
{
public:
    A()
    {   
        foo((T)0, 0);
    }

    template<class T2>
    void foo(T2, void* p) {}

    template<>
    void foo<bool>(bool, void* p)
    {
        t = (T)p;
    }
    T t;
};

int main()
{
    A<bool> a;
    return 0;
}

What should I do to get this feature to work when using Clang, so I can compile the MSVC standard library? 使用Clang时,我应该怎么做才能使此功能起作用,以便可以编译MSVC标准库?

This is the compile error I get: 这是我得到的编译错误:

warning: explicit specialization of 'foo' within class scope is a Microsoft extension [-Wmicrosoft]
void foo<bool>(bool, void* p)

error: expected ';' after expression
                t = (T)p;
                       ^
                   ;
error: no member named 'T' in 'A<bool>'
                t = (T)p;
                     ^

AFAIK you will have to extend clang to support explicit specialization within class scope. AFAIK您将必须扩展clang来支持类范围内的显式专业化。 I don't think there is any way around it, unless you want to modify the library. 我认为没有办法解决它,除非您想修改库。

I think you'll have three possibilities: 我认为您将有三种可能性:

  1. Rewrite the MSVC library so it does not use that extension (could be fun, but time consuming...) 重写MSVC库,使其不使用该扩展名(可能很有趣,但是很耗时...)
  2. Fix that MS-extension in Clang (could be even more fun and time consuming...) 修复了Clang中的MS扩展(可能会更加有趣和耗时...)
  3. Do the sane thing: use each library with the compiler it was made for (how boring...) 做理智的事情:将每个库与为其使用的编译器一起使用(多么无聊...)

Update from this testing code for Clang I assume that it is already implemented, but it does not expect an explicit template parameter: 更新 对于锵测试代码,我认为它已经实现,但它并不期望一个明确的模板参数:

template<>
void foo(bool, void* p)
{
    t = (T)p;
}

Its probably a bug in Clang's implementation of that extension. 这可能是Clang对该扩展的实现中的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM