2016-10-04 54 views
1

這是前一個問題here的後續行動。基本上,下面的程序在clang中使用別名模板時,有沒有辦法縮短模板化的類名?

#include <memory> 

// Create a class parameterized on a template 
template <template <typename> class XX> 
struct Foo{}; 

// Some template with a long name 
template <typename T> 
struct ReallyLongFileNameThatIHateToType { 
}; 

// Alias to shorten name 
template <typename T> 
using Bar = ReallyLongFileNameThatIHateToType <T>; 

int main() { 
    std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>> foo = 
     std::make_unique<Foo<Bar>>(); 
} 

作品只是用gcc罰款;鐺

g++ -std=c++14 test04.cpp -o test04 
clang++ -std=c++14 test04.cpp -o test04 
test04.cpp:20:61: error: no viable conversion from 'unique_ptr<Foo<template 
     Bar>>' to 
     'unique_ptr<Foo<template ReallyLongFileNameThatIHateToType>>' 
    std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>> foo = 
                  ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:200:17: note: 
     candidate constructor not viable: no known conversion from 'typename 
     _MakeUniq<Foo<Bar> >::__single_object' (aka 'unique_ptr<Foo<Bar> >') to 
     'nullptr_t' for 1st argument 
     constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { } 
       ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:205:7: note: 
     candidate constructor not viable: no known conversion from 'typename 
     _MakeUniq<Foo<Bar> >::__single_object' (aka 'unique_ptr<Foo<Bar> >') to 
     'std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>, 
     std::default_delete<Foo<ReallyLongFileNameThatIHateToType> > > &&' for 1st 
     argument 
     unique_ptr(unique_ptr&& __u) noexcept 
    ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:356:7: note: 
     candidate constructor not viable: no known conversion from 'typename 
     _MakeUniq<Foo<Bar> >::__single_object' (aka 'unique_ptr<Foo<Bar> >') to 
     'const std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>, 
     std::default_delete<Foo<ReallyLongFileNameThatIHateToType> > > &' for 1st 
     argument 
     unique_ptr(const unique_ptr&) = delete; 
    ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/type_traits:1957:41: note: 
     candidate template ignored: disabled by 'enable_if' [with _Up = Foo<Bar>, 
     _Ep = std::default_delete<Foo<Bar> >] 
    using _Require = typename enable_if<__and_<_Cond...>::value>::type; 
             ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:228:2: note: 
     candidate template ignored: could not match 'auto_ptr' against 
     'unique_ptr' 
     unique_ptr(auto_ptr<_Up>&& __u) noexcept; 
     ^
1 error generated. 
Makefile:2: recipe for target 'all' failed 
make: *** [all] Error 1 

由於聯動消息中指出的那樣,這是CWG問題1244不管怎麼說,有另一種方式來縮短與clang一起使用的名字?通常,我在模板類上有參數化代碼,但我不想一遍又一遍地輸出完整的參數名稱。在使用gcc時,我只需創建別名模板並在一天內調用它。顯然,在叮噹聲中,這是行不通的,那麼是否有不同的機制來實現同樣的效果?

+0

'using FooBar = Foo '? – Barry

+0

'auto foo = std :: make_unique >();'? – skypjack

回答

0

作爲一種解決方法,您可以通過將模板模板參數傳遞給某個輔助結構以使用模板模板參數將其設置爲一種解決方法。 (在clang ++,C++ 14中工作):

#include <memory> 

// Create a class parameterized on a template 
template <template <typename> class XX> 
struct Foo{}; 

// Some template with a long name 
template <typename T> 
struct ReallyLongFileNameThatIHateToType { 
}; 

template <template <template <class> class> class FF> 
struct Bar { 
    using type = FF<ReallyLongFileNameThatIHateToType>; 
}; 

template <template <template <class> class> class FF> 
using Bar_t = typename Bar<FF>::type; 


int main() { 
    std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>> foo = 
     std::make_unique<Bar_t<Foo>>(); 
} 
相關問題