English 中文(简体)
为什么在C++11的类型参数包装之后使用一个整体价值参数包装?
原标题:Why is using an integral value parameter pack disallowed after a type parameter pack in C++11?

问题几乎没有意义。 因此,我试图这样做。

总的来说,C++允许:

template<class T, class U, T t, U u>
void func() {}

func<char, int,  A , 10>();

但是,似乎自然的理论延伸并不奏效。

template<class...T, T... t>
void func() {}

func<char, int,  A , 10>(); 

Both clang and g++4.7 reject the above code. The error is shown where the instantiation is done. It appears to me that the two variadic lists should be parsed unambiguously because the first one has types and the other one has integral values only.

If the above is not meant to work, I think the following won t work either.

template <class Ret, class... Args, Ret (*func)(Args...)>
class Foo {};

我认为,“Foo”模板是一个非常有用的东西。

最佳回答

(Extra: 直接回答你的第一个问题,你还可以将<条码>template<}...T. t> void func(){}变成一个模板,在边段。 这在++4.6中并不奏效,但在3.0部族中则如此。

在模板中添加一个模板:

template<class ... T>
struct func_types {
   template <T ... t>
   static void func_values() {
       // This next line is just a demonstration, and 
       // would need to be changed for other types:
       printf("%c %d
", t...);
   }
};

int main() {
    func_types<char, int> :: func_values< A , 10>();
}

是否可接受一个模版。 另一种选择是使用带<代码>func< tuple<char,int> ,_tuple(A,10) >的标记。 我认为这一点是可行的,但你可能不得不推出自己的补习班(这似乎使读本为constexpr)。

Finally, you might be able to implement your Foo template as follows:

template<typename Ret, typename ...Args>
struct Foo {
        template< Ret (*func)(Args...)>
        struct Bar {
                template<typename T...>
                Bar(T&&... args) {
                        cout << "executing the function gives: "
                           << func(std::forward(args)...) << endl;
                }
        };
};

int main () {
    Foo<size_t, const char*> ::  Bar<strlen> test1("hi");
    Foo<int, const char*, const char*> ::  Bar<strcmp> test2("compare","these");
}

This latter code is on ideone. To demonstrate, I implemented a constructor to forward the args to the function that s code into the template.

问题回答

因为没有人会认为应该有这一特点。 理论模板的设计意在简单易行。 其他可能先进和有用的特征也包括在内。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签