[llvm-bugs] [Bug 50585] New: Template class constructor inside cpp. Unnecessary type requirements

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jun 4 22:12:47 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50585

            Bug ID: 50585
           Summary: Template class constructor inside cpp. Unnecessary
                    type requirements
           Product: clang
           Version: 9.0
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: help0001 at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

hey guys,

Today playing with mac os compiling and clang 9.0. And apperntly i noticed a
bug in it.

Normally when you create a template like this:
example.cpp
```cpp
template<typename T>
void execute(T v)
{
    /* Your code */
}
```
Because execute isnt been used anywhere in the example.cpp this means this
function wont get compiled. You have to define it in your cpp file on the
bottom the following code to get it compiled:
```cpp
template void execute<int>(int v);
```

Now we have a basic understanding what is required we can go to the clang bug.
When you create a class constructor. It wont return any type. When you declare
the template type as before you see we have a void as a return value. And in
clang 9.0 it has a type as requirement. And this shouldn't be happening.

The following will fail in clang 9.0+ but it should be valid
```cpp
template CPubKey::CPubKey<unsigned char*>(unsigned char*, unsigned char*);
```
Error clang++ gives:
```
src/cpubkey.cpp:261:20: fatal error: qualified reference to 'CPubKey' is a
constructor name rather than a type in this context
```

Example case:
https://godbolt.org/z/o39MahhKz

I made the following fix myself.
cpubkey.h
```cpp
/** An encapsulated public key. */
class CPubKey
{
private:
    // Just store the serialized data.
    // Its length can very cheaply be computed from the first byte.
    unsigned char vch[65];

    // Compute the length of a pubkey with a given first byte.
    static unsigned int GetLen(unsigned char chHeader);

    // Set this key data to be invalid
    void Invalidate();

public:
    // Construct an invalid public key.
    CPubKey();

    // Construct a public key using begin/end iterators to byte data.
    #if defined(__clang__)
        template<typename T>
        CPubKey(const T pbegin, const T pend)
        {
            Set(pbegin, pend);
        }    
    #else // defined(__clang__)
        template<typename T>
        CPubKey(const T pbegin, const T pend);
    #endif // defined(__clang__)
```

cpubkey.cpp
```cpp
// Construct a public key using begin/end iterators to byte data.
#if !defined(__clang__)
    template<typename T>
    CPubKey::CPubKey(const T pbegin, const T pend)
    {
        Set(pbegin, pend);
    }

    template CPubKey::CPubKey<unsigned char*>(unsigned char*, unsigned char*);
#endif // !defined(__clang__)
```

Cases:
I manage to test this case on windows, mac and linux. version 9.0 till version
11.0. And all have the same related issue.

Extra:
I manage to discuss this issue on your Discord platform and one person manage
to verify that this is a issue clang++ has.
See here more related to it:
https://discord.com/channels/636084430946959380/636808521371090956/850316658605293578

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210605/909f5e24/attachment.html>


More information about the llvm-bugs mailing list