如何在main()
中编译最后一行?
#include <initializer_list>
#include <type_traits>
#include <functional>
template <typename T>
struct foo {
foo(std::initializer_list<T>) { }
template <typename C> struct is_foo : std::false_type { };
template <typename U> struct is_foo<foo<U>> : std::true_type { };
template <typename Compare>
std::enable_if_t<!is_foo<Compare>::value> bar(foo&, Compare comp) {
bool b = comp(T(), T()); // This line compiles thanks to is_foo<Compare>.
}
void bar(foo&& f) { bar(std::forward<foo>(f), std::less<T>()); }
template <typename... Foos>
void bar(Foos&&...) { }
};
int main() {
foo<int> f = {1,2,3};
f.bar({4,5,6}); // Compiles fine
f.bar(f,f); // Compiles fine (thanks to is_foo<Compare>)
f.bar(f,{4,5,6}); // Won't compile
}
它应该调用foo<T>::bar(Foos&&...)
。
Test.cpp:27:17: error: no matching function for call to 'foo<int>::bar(foo<int>&, <brace-enclosed initializer list>)'
f.bar(f,{4,5,6}); // Won't compile
^
Test.cpp:13:44: note: candidate: template<class Compare> std::enable_if_t<(! foo<T>::is_foo<C>::value)> foo<T>::bar(foo<T>&, Compare) [with Compare = Compare; T = int]
std::enable_if_t<!is_foo<Compare>::value> bar(foo&, Compare comp) {
^~~
Test.cpp:13:44: note: template argument deduction/substitution failed:
Test.cpp:27:17: note: couldn't deduce template parameter 'Compare'
f.bar(f,{4,5,6});
不确定你究竟想要什么,但是,如果你打算获得一个可变的bar()
函数,它接收零个或多个foo<T>
参数(或者可以用来初始化foo<T>
的参数),那么...如果你接受了一个限制参数的数量(比如63,在下面的例子中)有一个WF技巧我认为showed one time可以根据您的情况进行调整。
如果您定义了typer
模板别名
template <typename T, std::size_t>
using typer = T;
和一个递归的struct proBar
template <typename T, std::size_t N = 64U,
typename = std::make_index_sequence<N>>
struct proBar;
template <typename T, std::size_t N, std::size_t... Is>
struct proBar<T, N, std::index_sequence<Is...>> : public proBar<T, N-1U>
{
using proBar<T, N-1U>::bar;
void bar (typer<T, Is>... ts)
{ }
};
template <typename T>
struct proBar<T, 0U, std::index_sequence<>>
{
void bar ()
{ }
};
允许定义你需要的bar()
函数,你的模板struct foo
成为
template <typename T>
struct foo : public proBar<foo<T>>
{
using proBar<foo<T>>::bar;
foo (std::initializer_list<T>)
{ }
template <typename Compare>
auto bar (foo &, Compare comp) -> decltype(comp(T(), T()), void())
{ bool b = comp(T(), T()); }
};
以下是完整的编译示例
#include <initializer_list>
template <typename T, std::size_t>
using typer = T;
template <typename T, std::size_t N = 64U,
typename = std::make_index_sequence<N>>
struct proBar;
template <typename T, std::size_t N, std::size_t... Is>
struct proBar<T, N, std::index_sequence<Is...>> : public proBar<T, N-1U>
{
using proBar<T, N-1U>::bar;
void bar (typer<T, Is>... ts)
{ }
};
template <typename T>
struct proBar<T, 0U, std::index_sequence<>>
{
void bar ()
{ }
};
template <typename T>
struct foo : public proBar<foo<T>>
{
using proBar<foo<T>>::bar;
foo (std::initializer_list<T>)
{ }
template <typename Compare>
auto bar (foo &, Compare comp) -> decltype(comp(T(), T()), void())
{ bool b = comp(T(), T()); }
};
int main()
{
foo<int> f = {1, 2, 3};
f.bar({4, 5, 6});
f.bar(f, f);
f.bar(f, {4, 5, 6}); // now compile
}
这有效。
#include <initializer_list>
#include <type_traits>
#include <functional>
template <typename T>
struct foo {
foo(std::initializer_list<T>) { }
template <typename C> struct is_foo : std::false_type { };
template <typename U> struct is_foo<foo<U>> : std::true_type { };
template <typename Compare>
std::enable_if_t<!is_foo<Compare>::value> bar(foo&, Compare comp) {
bool b = comp(T(), T()); // This line compiles thanks to is_foo<Compare>.
}
void bar(foo&& f) { bar(std::forward<foo>(f), std::less<T>()); }
template <typename... Foos>
void bar(Foos&&...) { }
};
int main() {
foo<int> f = { 1,2,3 };
f.bar({ 4,5,6 }); // Compiles fine
f.bar(f, f); // Compiles fine (thanks to is_foo<Compare>)
f.bar(f, foo<int>({ 4,5,6 })); // Won't compile
}